Display data using drop down menu

Shows some Tutorials

Display data using drop down menu

Postby sofia on Fri Mar 21, 2008 10:20 pm

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.

Code: Select all
<?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
sofia
Fresh Boarder
 
Posts: 15
Joined: Sun Mar 09, 2008 2:36 pm

Re:Display data using drop down menu

Postby GreyHead on Fri Mar 21, 2008 10:35 pm

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.
Code: Select all
<?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
Bob Janes
info at greyhead.net
ChronoForms Support If you like ChronoForms please vote or post a review at Joomla.org
User avatar
GreyHead
Platinum Boarder
 
Posts: 3461
Joined: Tue May 29, 2007 10:15 pm
Location: Brittany

Re:Display data using drop down menu

Postby sofia on Fri Mar 21, 2008 11:05 pm

Hi Bob,

I have changed the code as you said. Now it's like this:
Code: Select all
<?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
sofia
Fresh Boarder
 
Posts: 15
Joined: Sun Mar 09, 2008 2:36 pm

Re:Display data using drop down menu

Postby GreyHead on Sat Mar 22, 2008 12:29 am

Hi Sofia,

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

Bob
Bob Janes
info at greyhead.net
ChronoForms Support If you like ChronoForms please vote or post a review at Joomla.org
User avatar
GreyHead
Platinum Boarder
 
Posts: 3461
Joined: Tue May 29, 2007 10:15 pm
Location: Brittany

Re:Display data using drop down menu

Postby sofia on Sat Mar 22, 2008 12:44 am

Done. Still only appears the select box after submit. No results.

Kiss,

Sofia
sofia
Fresh Boarder
 
Posts: 15
Joined: Sun Mar 09, 2008 2:36 pm

Re:Display data using drop down menu

Postby GreyHead on Sat Mar 22, 2008 1:28 am

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
Bob Janes
info at greyhead.net
ChronoForms Support If you like ChronoForms please vote or post a review at Joomla.org
User avatar
GreyHead
Platinum Boarder
 
Posts: 3461
Joined: Tue May 29, 2007 10:15 pm
Location: Brittany

Re:Display data using drop down menu

Postby GreyHead on Mon Mar 24, 2008 3:31 am

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:
Code: Select all
<?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:
Code: Select all

<?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
Bob Janes
info at greyhead.net
ChronoForms Support If you like ChronoForms please vote or post a review at Joomla.org
User avatar
GreyHead
Platinum Boarder
 
Posts: 3461
Joined: Tue May 29, 2007 10:15 pm
Location: Brittany

Re:Display data using drop down menu

Postby sofia on Mon Mar 24, 2008 10:29 pm

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
sofia
Fresh Boarder
 
Posts: 15
Joined: Sun Mar 09, 2008 2:36 pm

Re:Display data using drop down menu

Postby GreyHead on Mon Mar 24, 2008 11:03 pm

Hi Sofia,

Sorry to hear you've been sick.

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

Bob
Bob Janes
info at greyhead.net
ChronoForms Support If you like ChronoForms please vote or post a review at Joomla.org
User avatar
GreyHead
Platinum Boarder
 
Posts: 3461
Joined: Tue May 29, 2007 10:15 pm
Location: Brittany

Re:Display data using drop down menu

Postby sofia on Tue Mar 25, 2008 3:51 pm

Hi Bob,

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

Thanks,

Sofia
sofia
Fresh Boarder
 
Posts: 15
Joined: Sun Mar 09, 2008 2:36 pm

Re:Display data using drop down menu

Postby admin on Tue Mar 25, 2008 7:48 pm

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 :
Code: Select all
<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 :

Code: Select all
<?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
ChronoForms/ChronoConnectivity/ChronoComments Developer Thanks for using our components!
If you have any problems with any extension please tell us.
If you like any of our extensions please post a review at Joomla.org
Want users to submit their content to your website ? try Submit Story
Want to list/edit/delete your data ? try ChronoConnectivity
Want to have stylish AJAX comments ? try ChronoComments
User avatar
admin
Platinum Boarder
 
Posts: 3282
Joined: Mon Aug 14, 2006 5:29 am

Re:Display data using drop down menu

Postby sofia on Sat Mar 29, 2008 8:08 pm

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
sofia
Fresh Boarder
 
Posts: 15
Joined: Sun Mar 09, 2008 2:36 pm


Return to ChronoForms How To

Who is online

Users browsing this forum: swlabhot and 2 guests