toney Posted March 14, 2012 Share Posted March 14, 2012 I have a form that searches a database then outputs the data via php how do I take the info from the radio button and the age range and search the database with it <?php $max_age = 18; $ageOptions = "<option value='00'>From</option>\n"; for($age=1; $age<=$max_age; $age++) { $ageOptions .= "<option value='{$age}'>{$age}</option>\n"; } ?> <form name="child_info" action="selectdata.php" method="post" id="child_info"> <table width="444" align="center" > <tr> <td width="208">Choose Male or Female:</td> <td width="224"> <input type="radio" name="gender" value="male" /> Male <input type="radio" name="gender" value="Female" /> Female </td> </tr> <tr> <td>Choose age range:</td> <td> <select name="first_age" id="first_age"> <?php echo $ageOptions; ?> </select> <select name="second_age" id="second_age"> <?php echo $ageOptions; ?> </select> </td> </tr> <tr> <td></td> <td> <div align="right"> <input type="submit" name="submit" id="submit" value="submit" /> <input type="reset" name="reset" id="reset" value="reset" /> </div> </td> </tr> </table> </form> <?php $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("", $con); $query = "SELECT picture_number, first_name, middle_name, first_family_name, second_family_name, DATE_FORMAT(birthdate, '%c-%e-%Y') as birthdate, gender FROM child_info ORDER BY picture_number ASC"; $result = mysql_query($query); if(!$result) { echo "There was a problem getting the data"; } else if(!$result) { echo "There were no results"; } else { echo "<b><center>Children to be sponsored</center></b><br><br>\n"; while($row = mysql_fetch_assoc($result)) { echo "<table border='1'> <tr> <th>Picture Number</th> <th>First Name</th> <th>Middle Name</th> <th>First Family Name</th> <th>Second Family Name</th> <th>Birthdate<br> M-D-Y</th> <th>Gender</th> </tr>"; { echo "<tr>"; echo "<td>" . $row['picture_number'] . "</td>"; echo "<td>" . $row['first_name'] . "</td>"; echo "<td>" . $row['middle_name'] . "</td>"; echo "<td>" . $row['first_family_name'] . "</td>"; echo "<td>" . $row['second_family_name'] . "</td>"; echo "<td>" . $row['birthdate'] . "</td>"; echo "<td>" . $row['gender'] . "</td>"; echo "</tr>"; } echo "</table>"; } } mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/258928-form-use-and-php-output/ Share on other sites More sharing options...
AyKay47 Posted March 14, 2012 Share Posted March 14, 2012 By grabbing the values the form sends to "selectdata.php" via $_POST. Where is the code that handles this form? Quote Link to comment https://forums.phpfreaks.com/topic/258928-form-use-and-php-output/#findComment-1327406 Share on other sites More sharing options...
toney Posted March 14, 2012 Author Share Posted March 14, 2012 <?php $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("", $con); $query = "SELECT picture_number, first_name, middle_name, first_family_name, second_family_name, DATE_FORMAT(birthdate, '%c-%e-%Y') as birthdate, gender FROM child_info ORDER BY picture_number ASC"; $result = mysql_query($query); if(!$result) { echo "There was a problem getting the data"; } else if(!$result) { echo "There were no results"; } else { echo "<b><center>Children to be sponsored</center></b><br><br>\n"; while($row = mysql_fetch_assoc($result)) { echo "<table border='1'> <tr> <th>Picture Number</th> <th>First Name</th> <th>Middle Name</th> <th>First Family Name</th> <th>Second Family Name</th> <th>Birthdate<br> M-D-Y</th> <th>Gender</th> </tr>"; { echo "<tr>"; echo "<td>" . $row['picture_number'] . "</td>"; echo "<td>" . $row['first_name'] . "</td>"; echo "<td>" . $row['middle_name'] . "</td>"; echo "<td>" . $row['first_family_name'] . "</td>"; echo "<td>" . $row['second_family_name'] . "</td>"; echo "<td>" . $row['birthdate'] . "</td>"; echo "<td>" . $row['gender'] . "</td>"; echo "</tr>"; } echo "</table>"; } } mysql_close(); ?> this handles the form Quote Link to comment https://forums.phpfreaks.com/topic/258928-form-use-and-php-output/#findComment-1327410 Share on other sites More sharing options...
Maq Posted March 14, 2012 Share Posted March 14, 2012 In the future, place OR tags around your code. Quote Link to comment https://forums.phpfreaks.com/topic/258928-form-use-and-php-output/#findComment-1327411 Share on other sites More sharing options...
AyKay47 Posted March 14, 2012 Share Posted March 14, 2012 So the form is sending the data to the executing page? If so, simply action='' is fine in the form. You'll want something like this.. if(isset($_POST['submit'])) //make sure submit button has been clicked { $gender = $_POST['gender']; //radio button value $first_age = $_POST['first_age']; //dd value $second_age = $_POST['second_age']; //dd value $sql = "select fields from table where something = '$gender' and something_else = '$first_age' and something_else = '$second_age'"; //etc... } keep in mind that this is pseudo code providing a very basic example without the proper error checks etc.. Quote Link to comment https://forums.phpfreaks.com/topic/258928-form-use-and-php-output/#findComment-1327465 Share on other sites More sharing options...
toney Posted March 14, 2012 Author Share Posted March 14, 2012 if(isset($_POST['submit'])) //make sure submit button has been clicked { $gender = $_POST['gender']; //radio button value $first_age = $_POST['first_age']; //dd value $second_age = $_POST['second_age']; //dd value $sql = "SELECT picture_number, first_name, middle_name, first_family_name, second_family_name, DATE_FORMAT(birthdate, '%c-%e-%Y') as birthdate, gender = '$gender' and something_else = '$first_age' and something_else = '$second_age' FROM child_info ORDER BY picture_number ASC"; } I am not sure what to write in for this = '$gender' and something_else = '$first_age' and something_else = '$second_age' someone help me please and tell me if what I have incorporated looks right Quote Link to comment https://forums.phpfreaks.com/topic/258928-form-use-and-php-output/#findComment-1327468 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.