Dropdown Filter and Display - php

I've created a dropdown that pulls data from my database:
//Selecting Product Name for Dropdown
$sql = "SELECT DISTINCT TypeOther FROM PedalDirectory";
$rs = mysql_query($sql) or die(mysql_error());
echo "<select>";
while($row = mysql_fetch_array($rs)){
echo "<option value='".$row["TypeOther"]."'>".$row["TypeOther"]."</option>";
}mysql_free_result($rs);
echo "</select>";
but im not sure how to get the dropdown menu to show the information from the database. tried a couple things but seems i can only get all the records to display.
What I would like to do is, when you select an item from the dropdown menu I want that record to appear on the page.
any helps appreciated.

Suppose your dropdown is like
<select id="drop">
<option value="">Select one...</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
</select>
Use jQuery to get selected value
$("#drop").change(function () {
var end = this.value;
alert(end);
});

Thanks all. I figured it out.
<form>
<?php
//Selecting Pedal Type for Dropdown
$sql = "SELECT * FROM PedalDirectory";
$rs = mysql_query($sql) or die(mysql_error());
echo '<select onchange="showUser(this.value)">';
echo '<option value="">Select a pedal type:</option>';
while($row = mysql_fetch_array($rs)){
echo "<option value='".$row["PedalID"]."'>".$row["Type"]."</option>";
}mysql_free_result($rs);
echo "</select>";
?>
</form>

Related

Text box show value from Ajax

Using PHP, Ajax and Jquery, I'm able to create a cascading dependent drop down menu. But I want to have the third drop down to be a text box instead.
How can I do this?
Ajax Code
PHP code...
<?php
include('conn.php');
$query = $con->query("SELECT * FROM job_category group by category_name ");
$rowCount = $query->num_rows;
?>
Category:
<select name="category" id="category">
<option value="">Select Category</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['category_num'].'">'.$row['category_name'].'</option>';
}
}else{
echo '<option value="">Country not available</option>';
}
?>
</select>
<br>
Sub-Category:
<select name="sub_type" id="sub_type">
</select>
<br>
Priority:
<select name="priority" id="priority_level" >
</select>
?>
Please help, I'm having a hard time on this.

PHP & MySQLi - Show selected value two time in drop-down list

I created my profile page with show all data in form from MySQL. All data is show rightly on form and also in drop-down list. But the problem is selected value is shows two time in the option list.
Here is the my code:
<select class="form-control" name="country" id="country">
<option value="">Select Country
<?php
//Get country list from Country master
$qry = "select * from country_master";
//Execute query
$result = mysqli_query($conn, $qry);
//Assigned fetched array to $Country
while($country = mysqli_fetch_array($result))
{
echo "<option value='$country[1]'>$country[1]</option>";
//Compare User Country with country list. $row[4] is the country column in user table
if($row[4] == $country[1])
echo "<option value='$country[1]' selected='selected'>$country[1]</option>";
}
?>
</option>
</select>
You need to change your while code like below:-
while($country = mysqli_fetch_array($result)){
//Compare User Country with country list. $row[4] is the country column in user table
if($row[4] == $country[1]){
echo "<option value='$country[1]' selected='selected'>$country[1]</option>";
}else{
echo "<option value='$country[1]'>$country[1]</option>";
}
}
Note: In your code first option is created and then condition is checked, that's why two times it will show the selected option.
The correct answer to the issue you are facing is provided by A-2-A.
Additionally
You are nesting all of your looped options inside your "Select Country" option. you should remove the last</option> tag before the </select> tag and move it after "Select Country" like so:
<option value="">Select Country</option>
This should fix your issue:
<select class="form-control" name="country" id="country">
<option value="">Select Country
<?php
//Get country list from Country master
$qry = "select * from country_master";
//Execute query
$result = mysqli_query($conn, $qry);
//Assigned fetched array to $Country
while($country = mysqli_fetch_array($result))
{
echo "<option value='$country[1]'".($row[4] == $country[1] ? " selected" : "").">$country[1]</option>";
}
?>
</option>
The problem was that you had echoed the selected option additionally to the non-selected option. Now, it adds the 'selected' attribute if the option should be selected. (Based on your condition)
It should be set up like this:
<select class="form-control" name="country" id="country">
<option value="">Select Country
<?php
//Get country list from Country master
$qry = "select * from country_master";
//Execute query
$result = mysqli_query($conn, $qry);
//Assigned fetched array to $Country
while($country = mysqli_fetch_array($result))
{
//Compare User Country with country list. $row[4] is the country column in user table
if($row[4] == $country[1]){
echo "<option value='$country[1]' selected='selected'>$country[1]</option>";
}
else{
echo "<option value='$country[1]'>$country[1]</option>";
}
}
?>
</option>
</select>
Because you need to check if the value is selected, then if it isn't display the data accordingly.
You have two issues:
using option inside the option.
second you just need to print selected attribute instead of printing complete option.
Example:
<option value="">Select Country
</option>
<?php
//Get country list from Country master $qry = "select * from country_master";
//Execute query
$result = mysqli_query($conn, $qry);
//Assigned fetched array to $Country
while($country = mysqli_fetch_array($result)) {
if($row[4] == $country[1]) {
$selected = 'selected=""';
}
else{
$selected = "";
}
?>
<option <?php echo $selected;?> value='<?php echo $country[1];?>'>
<?php echo $country[1];?>
</option>
<?php
}
?>

How to populate a combo-box in EDIT FORM

please help me. I include here (see bellow) the Combobox script for INSERTING FORM
I have a Form of Adding a new product. One of option is to select what Category. So, Combobox is for selecting Category from.
<TD>
<?php
$sql="SELECT categories.id as id, categories.name as name FROM categories";
$result=mysql_query($sql) or die(mysql_error());
$options="";
while ($row=mysql_fetch_assoc($result)) {
$id=$row["id"];
$thing=$row["name"];
$options.= " <OPTION VALUE=".$id.">".$thing.'</option>';
}
?>
<select name="CATEGORY" onClick=”submitCATEGORY();”>
<option value="0">Select Category
<?php echo $options;?></option>
</select>
</TD>
Now, I would like to have an EDIT FORM using the same script as for Inserting data in database using combobox.
<?php echo $CATEGORY; ?> this script is for retrieving the data from database.
please help me to find a way, when I want to Edit a PRODUCT information to get combobox with option I selected during the inserting the data...
I could succeed to fill the data for the name of products and other information, only Combobox is empty. I hope you could understand what I want to achieve! Thank you in advance for your time!!!
See bellow what I tried but did not succede:
<?php
$sql="SELECT categories.id as id, categories.name as name FROM categories";
$result=mysql_query($sql) or die(mysql_error());
$options="";
while ($row=mysql_fetch_assoc($result)) {
$id=$row["id"];
$thing=$row["name"];
$options.= " <OPTION VALUE=".$id.">".$thing.'</option>';
}
?>
<select name="CATEGORY" onClick=”submitCATEGORY();”>
<option value="<?php echo $CATEGORY; ?>">
<?php echo $options;?></option>
</select>
</TD>
Try the following in your edit page,
<?php
$CATEGORY = 3; //from DB table, consider 3 as category id for sample
$sql="SELECT categories.id as id, categories.name as name FROM categories";
$result=mysql_query($sql) or die(mysql_error());
$options="";
while ($row=mysql_fetch_assoc($result)) {
$id=$row["id"];
$thing=$row["name"];
$isSel = ($CATEGORY == $id)?"selected":'';
$options.= " <OPTION VALUE='$id' $isSel>$thing</option>';
}
?>
<select name="CATEGORY" onClick=”submitCATEGORY();”>
<option value="<?php echo $CATEGORY; ?>">
<?php echo $options;?></option>
</select>
</TD>
If I understood you correctly then on Edit form you should mark the option that should be selected with 'selected ' tag:
<Option value="2" selected="selected">2</Option>
Tryt this..If I understood corectly than:-
<option value="your_id" <?php echo $CATEGORY == your_id ?'selected':'';?>>your_category_name</option>
here $CATEGORY will be the retriving data from table
for your edit page you should do like this:-
<option value="1" <?php echo $CATEGORY == 1 ?'selected':'';?> ><?php echo $options;?></option>

Show selected value in dropdown after submit

hi guys i added dropdown field to form however after i submit the form if there is any error dropdown resets itself how can keep to value after validation thanks a lot for your any helps and idea
here is my code
<td><select id="country" name="country" style="width:150px;">
<option value="-1">Select</option>
<?php
$query = "SELECT country_id, name FROM countries ";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo "<option value=\"".$row['country_id']."\" >".$row['name']."</option>\n ";
}
?>
</select></td>
<td><?php echo $form->error("country"); ?></td>
Typically you would set the default option with attribute selected that is tied to the current selected value. So in this case, the option that equals the value of $_POST['country']:
while ($row = mysql_fetch_array($result))
{
if ($row['country_id'] == $_POST['country'])
$selected = "selected=\"selected\"";
else
$selected = "";
echo "<option value=\"".$row['country_id']."\" $selected>".$row['name']."</option>\n ";
}
Which would render as the following on the appropriate option:
<option value="123" selected="selected">456</option>

How to populate a particular column list in Combo Box with PHP?

I am using the given code below to populate some values from a column of a table. It's just getting filled blank..
Can you please find the problem with it ?
<select name="category">
<option value="" selected>Select a category</option>
<?php
mysql_connect("localhost","root","");
mysql_select_db("muskilaasaan");
$category = "SELECT cat FROM category";
$query_result = mysql_query($category);
while($result = mysql_fetch_array($query_result))
{
?>
<option value = "<?php echo $result['cat']?>"/>
<?php
}
?>
</select>
<select name="category">
<option value="" selected>Select a category</option>
<?php
mysql_connect("localhost","root","");
mysql_select_db("muskilaasaan");
$category = "SELECT cat FROM category";
$query_result = mysql_query($category);
while($result = mysql_fetch_assoc($query_result))
{
?>
<option value = "<?php echo $result['cat']?>"><?php echo $result['cat']?></option>
<?php
}
?>
</select>
Changed to mysql_fetch_assoc and also you didn't put anything in the option tags, that would cause it to appear blank.
it does not help if you specify the connection string as a second argument in mysql_select_db() ? see here for an example: http://php.net/manual/de/function.mysql-select-db.php

Categories