Forums

Display data using drop down menu

sofia 21 Mar, 2008
Hello guys,

I need some help.

I have two forms, one to collect data the other to display it. It's working great. Except for one thing. I want to have at the top of the table a drop down menu so people can choos e a district near their house, so they see records only from that area. This is the code i have. I manually assign $escolha to viseu, and it works. But what i really want i for the value to be equal to whatever choosed in select menu. I can't make this work.

<?php
// initialise the database code
global $database;
$escolha = "viseu";

$sql = "SELECT * FROM #__chronoforms_2 WHERE distrito = '$escolha';";
$database->setQuery($sql);
$rows = $database->loadObjectList();
echo "<table width=\"500px\">
      <tr><td valign=\"middle\"><h1>Distrito</h1></td>
	      <td valign=\"middle\">
		  <select name=\"lista\" size=\"1\" id=\"lista\">
			<option value=\"vazio\" selected>choose one</option>		  
			<option value=\"Lisboa\">Lisboa</option>
			<option value=\"Viseu\">Viseu</option></select> <input type=\"submit\" name=\"submit\" value=\"Test!\"> </td></tr>";	  
 

foreach ( $rows as $record ) {
  echo "<tr>
  		  <td colspan=\"2\"><h2>".$record->cf_id." | ".$record->especie." | ".$record->raca."</h2></td></tr>
		<tr>
  		  <td valign=\"top\">Em ".$record->data.", ".$record->nomeanimal.", tinha:<br />
		    Idade: ".$record->idade." anos<br />
		    Peso: ".$record->peso." kg<br />
			Altura: ".$record->altura." cm<br /></td>
  		  <td valign=\"top\">Reside com: ".$record->user.", ".$record->distrito."<br />
			Telefone: ".$record->telefone."<br />
			Email: ".$record->email."<br /></td>
 		</tr>";
}
echo "</table>";


You can see the form here

I have seen the post here but don't seams to work for me. I have somehing wrong.

I know this as nothing to do with chronoforms. The problem is that i'm no good writing php, but still, can you give me a hand?

I trying to work this for a week and no solution.<br><br>Post edited by: GreyHead, at: 2008/03/21 18:24
GreyHead 21 Mar, 2008
Hi Sofia,

My earlier post was rubbish - you can't look up a select entry in the database in the middle of a form like that. Sorry, my brain can't have been connected that day.

To do this with ChronoForms you'd need to submit after the district is selected then re-show this form (or a new one) with the additional fields.
<?php
// initialise the database code
global $database;
if ( !$_POST['escolha'] ) {
  // show select list
} else {
  $escolha = $_POST['escolha'];
  $sql = "
    SELECT * 
      FROM #__chronoforms_2 
      WHERE distrito = '$escolha';";
  $database->setQuery($sql);
  $rows = $database->loadObjectList();
  echo "<table width='500px'>
    <tr><td valign='middle'><h1>Distrito</h1></td>
    <td valign='middle'>
    <select name='lista' size='1' id='lista'>
    <option value='vazio' selected>choose one</option>          
    <option value='Lisboa'>Lisboa</option>
    <option value='Viseu'>Viseu</option>
    </select> 

<input type='submit' name='submit' value='Test!'> </td></tr>";

foreach ( $rows as $record ) {
  echo "<tr>
    <td colspan='2'>
    <h2>".$record->cf_id." | ".$record->especie." | ".$record->raca."</h2>
    </td></tr>
    <tr>
    <td valign='top'>Em ".$record->data.", ".$record->nomeanimal.", tinha:<br />
            Idade: ".$record->idade." anos<br />
            Peso: ".$record->peso." kg<br />
            Altura: ".$record->altura." cm<br /></td>
    <td valign='top'>Reside com: ".$record->user.", ".$record->distrito."<br />
            Telefone: ".$record->telefone."<br />
            Email: ".$record->email."<br /></td>
    </tr>";
  }
  echo "</table>";
}
?>
Bob<br><br>Post edited by: GreyHead, at: 2008/03/21 18:38
sofia 21 Mar, 2008
Hi Bob,

I have changed the code as you said. Now it's like this:
<?php
// initialise the database code
global $database;
if ( !$_POST['escolha'] ) {
  echo "<table width='500px'>
    <tr><td valign='middle'><h1>Distrito</h1></td>
    <td valign='middle'>
    <select name='escolha' size='1' id='escolha'>
    <option value='vazio' selected>choose one</option>          
    <option value='Lisboa'>Lisboa</option>
    <option value='Viseu'>Viseu</option>
    </select> 

<input type='submit' name='submit' value='Test!'> </td></tr>";

} else {
  $escolha = $_POST['escolha'];
  $sql = "
    SELECT * 
      FROM #__chronoforms_2 
      WHERE distrito = '$escolha';";
  $database->setQuery($sql);
  $rows = $database->loadObjectList();
  echo "<table width='500px'>
    <tr><td valign='middle'><h1>Distrito</h1></td>
    <td valign='middle'>
    <select name='escolha' size='1' id='escolha'>
    <option value='vazio' selected>choose one</option>          
    <option value='Lisboa'>Lisboa</option>
    <option value='Viseu'>Viseu</option>
    </select> 

<input type='submit' name='submit' value='Test!'> </td></tr>";

foreach ( $rows as $record ) {
  echo "<tr>
    <td colspan='2'>
    <h2>".$record->cf_id." | ".$record->especie." | ".$record->raca."</h2>
    </td></tr>
    <tr>
    <td valign='top'>Em ".$record->data.", ".$record->nomeanimal.", tinha:<br />
            Idade: ".$record->idade." anos<br />
            Peso: ".$record->peso." kg<br />
            Altura: ".$record->altura." cm<br /></td>
    <td valign='top'>Reside com: ".$record->user.", ".$record->distrito."<br />
            Telefone: ".$record->telefone."<br />
            Email: ".$record->email."<br /></td>
    </tr>";
  }
  echo "</table>";
}
?>


I believe that the name inside $_POST['escolha'] must be the same as the name i gave to the select box (<select name='escolha'), right?

Still not working. I see the select box but after choosing a district and pressing test a blank page appears. You can test it here http://cp03.buyhttp.com/~insidepd/index.php?option=com_content&task=view&id=447&Itemid=104


Thanks,

Sofia
GreyHead 22 Mar, 2008
Hi Sofia,

Try setting the form redirect to index.php?option=com_chronocontact&chronoformname=bancodesangue_display

Bob
sofia 22 Mar, 2008
Done. Still only appears the select box after submit. No results.

Kiss,

Sofia
GreyHead 22 Mar, 2008
Hi Sofia,

I guess we lose the $_POST array in the redirect. I'll try building a version here in the morning and see if I can get it to work.

Bob
GreyHead 24 Mar, 2008
Hi Sofia,

I think this will work (fingers crossed).

In the general tab turn 'Email the results/' to OFF

In the Form code tab put this in Form HTML:
 <?php
global $database;
echo '<table width='500px'>
  <tr><td valign='middle'><h1>Distrito</h1></td>
  <td valign='middle'>
  <select name='escolha' size='1' id='escolha'>
    <option value='vazio' selected>choose one</option>          
    <option value='Lisboa'>Lisboa</option>
    <option value='Viseu'>Viseu</option>
  </select> 

<input type='submit' name='submit' value='Test!' />
</td></tr>';

if ( $_POST['escolha'] ) {
  $sql = '
    SELECT * 
      FROM #__chronoforms_23 
      WHERE distrito = ''.$_POST['escolha'].'';';
  $database->setQuery($sql);
  $rows = $database->loadObjectList();
  echo '<table width='500px'>';
  foreach ( $rows as $record ) {
    echo '<tr>
      <td colspan='2'>
      <h2>'.$record->cf_id.' | '.$record->especie.' | '.$record->raca.'</h2>
      </td></tr>';
     <tr>
    <td valign='top'>Em '.$record->data.', '.$record->nomeanimal.', tinha:<br />
            Idade: '.$record->idade.' anos<br />
            Peso: '.$record->peso.' kg<br />
            Altura: '.$record->altura.' cm<br /></td>
    <td valign='top'>Reside com: '.$record->user.', '.$record->distrito.'<br />
            Telefone: '.$record->telefone.'<br />
            Email: '.$record->email.'<br /></td>
    </tr>
  }
  echo '</table>';
}
?>
and in the OnSubmit after email box put this code:
 
<?php
showform( $_POST );
?>
Like this the form shows just the select box if nothing has been selected, otherwise it shows the select box and the results for the district.

Bob<br><br>Post edited by: GreyHead, at: 2008/03/23 23:34
sofia 24 Mar, 2008
Hello Bob,

I have been seek, so i only saw your message today. Tried and still get the same blank page😟

Email option was already disabled.

I replaced the code with yours and added the after submit code you sent, and still get the same blank page after submit.

Maybe it would be easier for you if i sent you throught pm a login and password so you can access my joomla backoffice, to check it out. Tell me if you want me to do it.

Big kiss,

Sofia
GreyHead 24 Mar, 2008
Hi Sofia,

Sorry to hear you've been sick.

Sure, - email me the login details to the address in my sig.

Bob
sofia 25 Mar, 2008
Hi Bob,

Can´t access greyhead.net fr your contacts because is down for maintenance. Can you send your mail to [email]piodesign@gmail.com[/email] and i'll reply to you with all information you will need?

Thanks,

Sofia
Max_admin 25 Mar, 2008
Hi Sofia,

First of all, dont go to the greyhead.net itself, instead, replace ' at ' with @😉

So, I have been looking into your post, I see the best way to do what you need is AJAX, If you can follow me you will be able to do it, here are the steps :

#1- I will assume you have 1 form which is showing the data called : show_data

put this code inside it :

<script type="text/javascript">		   
			function getHTTPObject() {
			  var xmlhttp;
			
			  if(window.XMLHttpRequest){
				xmlhttp = new XMLHttpRequest();
			  }
			  else if (window.ActiveXObject){
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"«»);
				if (!xmlhttp){
					xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"«»);
				}
			   
			}
			  return xmlhttp;			 
			}
			var http = getHTTPObject(); // We create the HTTP Object
	</script>
	<script type="text/javascript">
	function loadfields(){
		var lista = document.getElementById("lista"«»).value;
		http.open("POST", "index2.php?option=com_chronocontact&chronoformname=show_data_ajax" , true);
		http.onreadystatechange = function () 
		{   if (http.readyState == 1) {
				document.getElementById('sofia_div').innerHTML = 'LOADING';
			}
			else if (http.readyState == 4) {
				if(http.status==200) {
					document.getElementById('sofia_div').innerHTML = ''; 
					var results=http.responseText.split("*#*");
					var results=results[1].split("#999-#");
					if(results[0] == 'yes'){
						document.getElementById('sofia_div').innerHTML = results[1];
					} 
				}
			}
		};
		//http.send(null);		
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"«»); 	
		http.send("escolha=" + escape(lista));	
	}
	</script>


<table><tr><td valign="middle"><h1>Distrito</h1></td>
          <td valign="middle">
          <select name="lista" size="1" id="lista" onChange="loadfields()">
            <option value="vazio" selected>choose one</option>          
            <option value="Lisboa">Lisboa</option>
            <option value="Viseu">Viseu</option></select> <input type="submit" name="submit" value="Test!"> </td></tr></table>
			
<div id="sofia_div"></div>


#2- create another form called : show_data_ajax and add this code inside it :


 <?php
// initialise the database code
global $database;
$escolha = $_POST['escolha'];

$sql = "SELECT * FROM #__chronoforms_2 WHERE distrito = '$escolha';";
$database->setQuery($sql);
$rows = $database->loadObjectList();
echo "*#*"."yes"."#999-#";
echo "<table width=\"500px\">
      ";      
 

foreach ( $rows as $record ) {
  echo "<tr>
            <td colspan=\"2\"><h2>".$record->cf_id." | ".$record->especie." | ".$record->raca."</h2></td></tr>
        <tr>
            <td valign=\"top\">Em ".$record->data.", ".$record->nomeanimal.", tinha:<br />
            Idade: ".$record->idade." anos<br />
            Peso: ".$record->peso." kg<br />
            Altura: ".$record->altura." cm<br /></td>
            <td valign=\"top\">Reside com: ".$record->user.", ".$record->distrito."<br />
            Telefone: ".$record->telefone."<br />
            Email: ".$record->email."<br /></td>
         </tr>";
}
echo "</table>";
echo "*#*";
?>



now access the main show_data form and choose any item from the dropdown and voila, it should be working!!!🙂

Let me know how it will go!

Cheers

Max

Post edited by: admin, at: 2008/03/25 15:51

Post edited by: admin, at: 2008/03/25 15:57<br><br>Post edited by: admin, at: 2008/03/25 16:02
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
sofia 29 Mar, 2008
Hello guys,

Sorry for the late response. This past week as been a mess. I have been sick. Only now, i feeling better.

Thanks for your responses.

Max, i did not try out your ajax code, because Bob solve the problem before. He acess my backoffice and discover the problem. I prefer to have the code in php, because, although i'm not an expert i can read it and understand it, so i can apply it in the future without boring you guys again. Ajax is like chinese for me.

Anyway, i'm sure it will help someone outhere.

Again, thanks for you kindness and help.

Kiss,

Sofia
This topic is locked and no more replies can be posted.