Jump to content

Recommended Posts

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();
?>

Link to comment
https://forums.phpfreaks.com/topic/258928-form-use-and-php-output/
Share on other sites

<?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

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..

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.