Connecting Tech Pros Worldwide Forums | Help | Site Map

Passing results from Gender Drop down to PHP

Newbie
 
Join Date: Nov 2008
Posts: 3
#1: Nov 3 '08
I have a project I am porting to the Web from stand alone Access and Word with VB. I have been pleased with LAMP so far, but this one part of the project is killing me!

Basically what I want to do is take the gender from a drop down box, and based on the selection, I would like to populate the drop down box for relation - i.e. male would return values such as son, father, uncle and female would return daughter, mother, aunt. Although it may be overkill, I want the values returned from a database.

So far in the body of the form I have the following:
Expand|Select|Wrap|Line Numbers
  1. <form name = "personal" method="POST">
  2.  
  3. <div id="right"><div class="title">Family & Friends Information</div>
  4.  
  5.     <tr><td>Gender: </td><td><select id="famgenderid" name="famgender" default="Male">
  6.                     <option value="Male">Male</option>
  7.                     <option value="Female">Female</option>
  8.                 </select></td></tr>
  9.     <tr><td>Relationship: </td><td>
  10.                 <?php    
  11.                     $gender = "Male";//This is the part that needs to be fixed!!!!!!
  12.                     selectSubset($connection, "tblRelationship" , "ctlRelationship", "ctlGender" , "relation", $gender); 
  13.  
  14.                  ?>
  15.                 </td></tr>
  16.     </table>
  17. </div>
  18.  
And the php script is as follows:
Expand|Select|Wrap|Line Numbers
  1. //Selects a Subset of values to place in a pulldown box based on value of prior box selected e.g. Male = brother, father
  2. function selectSubset ($connection, $tableName , $attributeName1, $attributeName2 , $pulldownName, $gender)
  3.     {
  4.     $defaultWithinResultSet = FALSE;
  5.     $selectSubset = "SELECT {$attributeName1}
  6.                     FROM {$tableName}
  7.                     WHERE {$attributeName2} = '{$gender}'";
  8.  
  9.     //Run selectSubset on the databaseName
  10.     if(!($resultID = @ mysql_query ($selectSubset, $connection)))
  11.         mysql_error();
  12.     print "\n<select name=\"$pulldownName\">";
  13.     //Retrieve each row from the Query
  14.     while ($row = @ mysql_fetch_array($resultID))
  15.         {
  16.             $result = $row[$attributeName1];
  17.             if (isset($defaultvalue) && $result == $defaultvalue)
  18.                 // Yes, show as selected
  19.                 print "\n\t<option selected value=\"{$result}\">{$result}";
  20.             else
  21.                 // No, just show as an option
  22.                 print "\n\t<option value=\"{$result}\">{$result}";
  23.             print "</option>";
  24.         }
  25.             print "\n</select>";
  26.  

Newbie
 
Join Date: Nov 2008
Posts: 3
#2: Nov 3 '08

re: Passing results from Gender Drop down to PHP


In the example above I have hard coded "Male" to get it to work one way. I am unsure how to pass the value from the famgender drop down box (Male or Female) to the variable $gender.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#3: Nov 3 '08

re: Passing results from Gender Drop down to PHP


Hi.

Personally, I would use AJAX to do this, but this can easily be done using PHP.

The complication here is that PHP is server-side, which means that by the time your user even gets the chance to see the <select> drop-down, the PHP script will already have completed it's job.

What you need to do is have PHP submit the <select> box first, and hold of posting the rest of the code until the form has been submitted with a gender value before re-posting the data.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. <form name="genderForm" action="?" method="post">            
  2. <?php
  3. // Check if all the info has been submitted
  4. if(isset($_POST['submitButton'])) 
  5. {
  6.     // Process the form
  7.     echo "<h3>Complete!</h3>";
  8. }
  9.  
  10. // Check if the gender has been selected
  11. else if(isset($_POST['gender']) && $_POST['gender'] != "null") 
  12. {
  13.     // Print some gender specific content
  14.     if($_POST['gender'] == "male") {
  15.         echo 'Enter you wifes name: <input type="text" name="wifeName" /><br />';
  16.     }
  17.     else {
  18.         echo 'Enter you husbands name: <input type="text" name="hubbyName" /><br />';
  19.     }
  20.  
  21.     // And of course the submit button
  22.     echo '<input type="submit" name="submitButton" value="submitted" />';
  23. }
  24.  
  25. // Nothing has been sent.
  26. else 
  27. {
  28.     // Show the gender <select> box.
  29.     echo <<<HTML
  30. Gender: <select name="gender" onchange="javascript: this.parentNode.submit();">
  31.     <option value="null" selected="selected">--Select please--</option>
  32.     <option value="female">Female</option>
  33.     <option value="male">Male</option>
  34. </select>            
  35. HTML;
  36. }
  37. ?>
  38. </form>
  39.  
This simply prints a gender <select> box. Once you select a gender, it automatically submits the form and loads the gender-specific form elements.
Once the submit button has been pressed, it should process the data.

It probably won't work exactly like you want yours to work, but it should at least point you in the right direction.

Let me know if something is unclear.

P.S.
Please use [code] tags when posting your code examples. (See How to ask a question)

[code] ... Code goes here... [/code]

Thank you.
Moderator
Newbie
 
Join Date: Nov 2008
Posts: 3
#4: Nov 3 '08

re: Passing results from Gender Drop down to PHP


Thanks atli. That makes sense. I think my best bet is just to stay away from php for this function and use javascript so it is all client side. The need to access the mysql server just is not that great in this instance.
Reply