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!
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>
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 !!
You need to change the location of the name attribute. You now have it on the option element but it needs to be on the select element.
<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");
}
?>
Related
Html Form:
<form>
<select name="country[]" id="country" multiple>
<option value="any">any</option>
<option value="India">India</option>
<option value="Canada">Canada</option>
<option value="UK">UK</option>
<option value="USA">USA</option>
<option value="Australia">Australia</option>
</select>
</form>
PHP Code
<?php
$country = $_REQUEST['country'];
if($country=="")
$countrysql = "";
else
{
if($country == "Any") $countrysql = "";
else
{
$country = str_replace(",","','",$country);
$countrysql = " and Country in ('$country')";
}
}
$queryString = "SELECT * FROM register where $countrysql";
?>
I have created a form in PHP and I want to search multiple options. I already created table Register and a column Country. I am getting the result If I give single value. If I give multiple I am not getting the result. Please help.
You evaluate in if($country == "Any") the word Any is not equal to any in option <option value="any">any</option>
But I suggest this php code:
<?php
$country="";
$countryError="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["country"])){
$countryError = "Country is required";
}else{
$country = $_POST["country"];
}
if($country == "Any") {
$queryString = "SELECT * FROM register";
}else{
$queryString = "SELECT * FROM register where Country in ('$country')";
}
// Print the SQL string:
echo $queryString;
}
?>
The html tags:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<select name="country" id="country" multiple>
<option value="Any">any</option>
<option value="India">India</option>
<option value="Canada">Canada</option>
<option value="UK">UK</option>
<option value="USA">USA</option>
<option value="Australia">Australia</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
<span class="error"><?php echo $countryError;?></span>
i dont know exactly how i am supposed to explain my question. but i'll try my best. Im currently trying to make an update form. when user clicked on the edit icon they will be directed to the edit form and the value are carried. i used 'typeid' to carry the values. im having problem with my drop down. i used selected but the value duplicate.
dropdown
so im trying to solve this, but know idea how to solve it. i have to used php only and i am not an expert of php.
if (isset($_GET['typeid'])) {
$sql = "SELECT * FROM vehicletype WHERE id_vehicleType=" . $_GET['typeid'];
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_array($result);
if($row['status_vehicleType']==1){
$status = "Enabled";
}
else{
$status = "Disabled";
}
}
above are the typeid that i used to carry the values.
<div class="form-group">
<label>Choose Vehicle Type Status</label>
<select class="form-control" name="status" required class="form-control" value="<? php if(isset($row['status_vehicleType'])){ echo $status; } ?>">
<option value="">Select Vehicle Type</option>
<option value=<?php echo $row['status_vehicleType']; ?> <?php if($_GET["typeid"]==$row['status_vehicleType']){ ?> selected <?php } ?> ><?php echo $status; ?></option>
<option value="1">Enabled</option>
<option value="0">Disabled</option>
</select>
Here is simple answer for this question
if you have any other questions freely ask me thanks
<?php
function selected($x, $y) {
if ($x == $y) {
return " selected";
} else {
return "";
}
}
$sql = "SELECT * FROM vehicletype WHERE id_vehicleType=" . $_GET['typeid'];
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_array($result);
$q_status = $row['status_vehicleType'] ;
?>
<div class="form-group">
<label class="control-label text-inverse" for="name">Status</label>
<select class="form-control" name="q_status" id="q_status" required="" autocomplete="off" >
<option value="1" <?php echo selected($q_status,"1");?> >Enabled</option>
<option value="0" <?php echo selected($q_status,"0");?> >Disabled</option>
</select>
</div>
use below code, (for testing set $_GET['typeid']=somevalue):
$row=array();
if (isset($_GET['typeid'])) {
$sql = "SELECT * FROM vehicletype WHERE id_vehicleType=" . $_GET['typeid'];
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_array($result);
}
<div class="form-group">
<label>Choose Vehicle Type Status</label>
<select class="form-control" name="status" required class="form-control">
<option value="">Select Vehicle Type</option>
<option value="1" <?php if($row['status_vehicleType']==1){?>selected<?php }?>>Enabled</option>
<option value="0" <?php if($row['status_vehicleType']==0){?>selected<?php }?>>Disabled</option>
</select>
I am having quite a bit of difficulty getting some functions of a multi-functional tool to work. I want to be able to sort by name or catalog number, filter by category, or do an open-ended search. Here is the current code:
$sort = ($_POST['sort']) ? $_POST['sort'] : 'name';
$order = ($_POST['order']) ? $_POST['order'] : 'asc';
if($_POST['Search'])
{
$search = ($_POST['Search']);
$query_compounds = "select * from compounds where name like'%$search%' or catalog_number like'%$search%' or synonyms like'%$search%' or cas_number like'%$search%' or formula_weight like'%$search%' or molecular_formula like'%$search%'";
}
else if($_POST['category']) {
$category = ($_POST['category']);
$query_compounds = "select * from compounds where category = ".$category;
}
else {
$query_compounds = "select * from compounds order by " . $sort . " " . $order;
}
Later in the page, the following code is called upon:
<form name="SortForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" style="float:left;">
<select name="sort">
<option <?php if($sort == 'name'){ echo "selected"; }?> value="name">Name</option>
<option <?php if($sort == 'catalog_number'){ echo "selected"; }?> value="catalog_number">Catalog Number</option>
</select>
<select name="order">
<option <?php if($order == 'asc'){ echo "selected"; }?> value="asc">Ascending</option>
<option <?php if($order == 'desc'){ echo "selected"; }?> value="desc">Descending</option>
</select>
<input name="SortForm" type="submit" id="SortForm" value="Sort">
</form>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" style="float:left;">
<select name="category">
<option <?php if($category == 'Compounds'){ echo "selected"; }?> value="Compounds">Compounds</option>
<option <?php if($category == 'Glucuronides'){ echo "selected"; }?> value="Glucuronides">Glucuronides</option>
<option <?php if($category == 'Metabolites'){ echo "selected"; }?> value="Metabolites">Metabolites</option>
</select>
<input name="category" type="submit" id="category" value="Select">
</form>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET" style="float:left;">
<input id="Search" type="text" placeholder="Type here">
<input id="Search" type="submit" value="Search" name="Search">
</form>
Any assistance in this matter would be greatly appreciated.
You need to change that PHP ASAP. See the comments above about SQL injection. Your code is dangerous.
As for the filtering, let PHP do the heavy lifting for you by dynamically building the query. When you find yourself repeating stuff - e.g. your individual queries - that's a good indicator that you are on the wrong path. I suggest looking into a framework like Laravel. It takes care of a lot of boilerplate like this. Its Query Builder is terrific.
If you want to do this using plain PHP, then do something like:
$where = array();
if ( isset($_POST['Search']) ) {
$search = mysql_real_escape_string($_POST['Search']);
$where['Search'] = "( name LIKE('%".$search."%')
OR catalog_number LIKE('%".$search."%')
OR synonyms LIKE('%".$search."%')
OR cas_number LIKE('%".$search."%')
OR formula_weight LIKE('%".$search."%')
OR molecular_formula LIKE('%".$search."') )";
}
if ( isset($_POST['category']) ) {
$category = mysql_real_escape_string($_POST['category']);
$where['category'] = "category = '".$category."'";
}
if ( count($where) > 0 ) {
$where = ' WHERE '.implode(' AND ', $where);
}
$sql = 'SELECT * FROM compounds'.$where.' ORDER BY ...';
Note that I'm using mysql_real_escape_string() to sanitize the user inputs simply because I suspect you're still using mysql_ functions. You should be using mysqli_ functions at a minimum.
Using the strategy above you can filter by as many (or few) variables as you'd like.
I am looking for a way to search for data from the database using input type box list, I tried make the code but it doesn't display anything:
html code:
<form action="users.php" method="post" name="searching">
<select name="users">
<option selected="selected" value="">-- select --</option>
<option value="1">user1</option>
<option value="2">user2</option>
<option value="3">user3</option>
</select>
<input type="submit" name="search" value="find">
</form>
php code:
if (isset($_POST['users'])) {
$key = trim ($_POST['users']);
$s = "SELECT * FROM users where user_name LIKE '%$key %'";
$res = mysql_query($s) or die('query did not work');
while($row = mysql_fetch_array( $res ))
{
?>
User ID: <?php echo $row['user_id'] ?>
User Name: <?php echo $row['user_name'] ?>
<?php
}
?>
when I try the code I didn't get any result and when I remove the while loop and put this instead of it :
<?php echo $key; ?>
it gives me the numbers of the selected value, for example if I select user2 the result will be 2. and I want the result to be user id and user name.
you need to fetch all the user name in your drop down select box
<select name="users">
<option selected="selected" value="">-- select --</option>
<?php $s2 = "SELECT * FROM users";
$q2=mysql_query($s2) or die($s2);
while($rw=mysql_fetch_array($q2))
{
echo '<option value="'.$rw['userid'].'">'.$rw['username'].'</option>';
}</select>
?>
<?php if (isset($_POST['search'])) { // submit button name here
$key = $_POST['users'];
$s = "SELECT * FROM users where user_id='".$key."'";
$res = mysql_query($s) or die($s);
while($row = mysql_fetch_array( $res ))
{
?>
User ID: <?php echo $row['user_id'] ?>
User Name: <?php echo $row['user_name'] ?>
<?php
}
?>
edit your html to this,you will get the in $_POST which will be in value='something'
<form action="users.php" method="post" name="searching">
<select name="users">
<option selected="selected" value="">-- select --</option>
<option value="user1">user1</option>
<option value="user2">user2</option>
<option value="user3">user3</option>
</select>
<input type="submit" name="search" value="find">
</form>
Or if value is the id of user then change query to this
$s = "SELECT * FROM users where user_id='".$key."'";
I have this form:
<form method='POST' action='update_ads.php'>
<select size='1' name='type'>
<OPTION value=''></OPTION>
<OPTION value='1'>Open</OPTION>
<OPTION value='0'>Closed</OPTION>
</select>
<input type='submit' value='Save'>
</form>
and this PHP file associated with it:
$id = $_POST['id'];
$type = addslashes($_POST['type']);
$query = mysql_query("UPDATE ads SET type='$type' where id=$id");
What can I add so that the option that the user choose before they clicked "submit" comes back when they re-open it? Thanks!
You could do it this way:
<?php
if (isset($_POST['id']) && ((int) $_POST['id'])) != 0) {
$id = $_POST['id'];
}
//connect to db
//get the responce of the query "SELECT `type` FROM `ads` WHERE `id` = $id"
//put it in a variable named $type
?>
<form method="POST" action="">
<select size='1' name='type'>
<option value="" <?=($type == '') ? 'selected="selected"' : ''?>></option>
<option value="1" <?=($type == 1) ? 'selected="selected"' : ''?>>Open</option>
<option value="0" <?=($type == 2) ? 'selected="selected"' : ''?>>Closed</option>
</select>
</form>
Edit: This is good ony if you have a select with not many options, otherwise you should use somethink like a foreach statement.
For many options you can use this:
<?php
$options = array('Open' => 1, 'Closed' => 0, 'Select an option' => ''); ?>
<form method="POST" action="">
<select size='1' name='type'>
<?php
foreach($options as $label => $option) {
if ($type == $option) {
$checked = 'selected="selected"';
}
else {
$checked = '';
}
?>
<option value="<?=$option?>" <?=$checked?>><?=$label?></option>
<?php
}
?>
</select>
</form>
Put this form in a string ($form) and do this
$form = str_replace(" value='".$id."'>"," value='".$id."' selected>", $form);
echo $form;
You can memorize the $id in a cookie.
when the user loads the page, you have to check the database for the current selection.
then you change the html based on the selection:
<OPTION value='1' selected='selected'>Open</OPTION>
<OPTION value='0'>Closed</OPTION>
or
<OPTION value='1'>Open</OPTION>
<OPTION value='0' selected='selected'>Closed</OPTION>
by the way, your ID has not been set in this form. you need to escape that as well
Not really the best solution, but it don't really know where this is going.
<select size="1" name="type">
<option value=""></option>
<option value="1"<?= $_POST['type']==1 ? " selected" : ""?>></option>
<option value="0"<?= $_POST['type']==0 ? " selected" : ""?>></option>
</select>
BTW: You might want to take a look at this.