I want to filter my data by countries from a dropdown menu:
<form name="filter_form" method="POST" action="display_data.php">
Select a country:
<select name="value">
<option name="country" value="AU">Austria</option>
<option name="country" value="BE">Belgium</option>
<option name="country" value="BU">Bulgaria</option>
</select>
<input type="submit" name="btn_submit" value="Submit Filter" />
<?php
if($_POST['country'] == 'BE') {
$query = mysql_query("SELECT * FROM think WHERE Country='Belgium'");
}
elseif($_POST['country'] == 'AU') {
$query = mysql_query("SELECT * FROM think WHERE Country='Austria'");
} else {
$query = mysql_query("SELECT * FROM think");
}
?>
The code does not filter any data. If anyone can help, thanks!
Avoid writing redundant code. Change your code with below code:
<form name="filter_form" method="POST" action="display_data.php">
Select a country:
<select name="country">
<option value="AU">Austria</option>
<option value="BE">Belgium</option>
<option value="BU">Bulgaria</option>
</select>
<input type="submit" name="btn_submit" value="Submit Filter" />
<?php
if(isset($_POST['country']))
{
switch($_POST['country'])
{
case 'BE' : $countryName = 'Belgium';
break;
case 'AU' : $countryName = 'Austria';
break;
default : $countryName = '';
break;
}
$where = '';
if($countryName != '')
{
$where = "WHERE Country='".$countryName."'";
}
$query = mysql_query("SELECT * FROM think ".$where."");
}
?>
if($_POST['country'] == 'BE') {
should be
if($_POST['value'] == 'BE') {
And so on for others !!
When you use select tag, the Server page will refer name of select tag and not option.
Change your code as below:
<select name="country">
<option value="AU">Austria</option>
<option value="BE">Belgium</option>
<option value="BU">Bulgaria</option>
</select>
<form name="filter_form" method="POST" action="display_data.php">
Select a country:
<select name="country">
<option value="AU">Austria</option>
<option value="BE">Belgium</option>
<option value="BU">Bulgaria</option>
</select>
<input type="submit" name="btn_submit" value="Submit Filter" />
<?php
if($_POST['country'] == 'BE') {
$query = mysql_query("SELECT * FROM think WHERE Country='Belgium'");
}
elseif($_POST['country'] == 'AU') {
$query = mysql_query("SELECT * FROM think WHERE Country='Austria'");
} else {
$query = mysql_query("SELECT * FROM think");
}
?>