How to populate a combo-box in EDIT FORM - php

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>

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.

Select form - use last posted value if blank

Sorry in advance for the novice question here...
I currently have my first value as a disabled and defaulted "Select" option which then changes to the selected option when a selection is made.
However if the user submits again without reselecting, the value defaults back because the post is blank. Therefore is there a way to use the previous value if so?
<select name="test_select" style="width: 110px">
<option disabled="disabled" selected="selected">
<?php
if(!empty($_POST['test_select'])){
echo $_POST[test_select'];}
else
echo "Select Option"; ?>
</option>
<?php $sql = mysql_query("SELECT test FROM test_settings");
while ($row = mysql_fetch_array($sql)){
?><option><?php echo $row['test']; ?></option><?php }?>
</select>
Thanks in advance,
Dan
I suppose that problem is that forms are not sending disabled values.
I would edit code as following:
<select name="test_select" style="width: 110px">
<?php
if (empty($_POST['test_select']))
echo '<option selected="selected">Select Option</option>';
$sql = mysql_query("SELECT test FROM test_settings");
while ($row = mysql_fetch_array($sql)){
$selected = isset($_POST['test_select']) && $row['test'] == $_POST['test_select']
? ' selected="selected"'
: '';
echo '<option'.$selected.'>'.$row['test'].'</option>';
?>
</select>

Dropdown Filter and Display

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>

listbox value goes 0 in database if not selected on edit mode in php

I have three tables
City: city_id, city_name
State: State_id, State_name
News: City_id, State_id, Headline, Story, Author etc.
I am Displaying data in textbox and listbox on edit button click Of Selected ID.
Data is displaying in listbox properly of selected id but when i click save button without change of listbox value it goes 0 in database.
If I don't change the listbox value and click save then it inserting 0 from listbox.
<?php
$data = 0;
if(isset($_GET['edit']))
{
$id = clean($_GET['edit']);
mysql_set_charset('utf8');
$sql="SELECT city_name,state_name,category_name,headline,author,story,source,photo,date from news left join
city on news.city_id=city.city_id left join state on news.state_id=state.state_id left join category on news.cat_id=category.category_id where id = '$id'";
$result=mysql_query($sql);
$data=mysql_fetch_array($result);
}
?>
<?php
$cat = $Admin->select($Admin->cat_table,'','','');
?>
<select name="cat_id" class="select" required="">
<option value="<?php if(isset($_GET['edit'])){ echo $data['category_name']; }?>"><?php if(isset($_GET['edit'])){echo $data['category_name'];}?></option>
<?php
foreach($cat as $load_category)
{
?>
<option value="<?php echo $load_category["category_id"]; ?>"><?php echo $load_category["category_name"]; ?></option>
<?php }?>
</select>
<?php
$errors = array();
$Admin = new admins;
if(isset($_POST['save']))
{
$table = $Admin->news_table;
if(isset ($_GET['edit']))
{
$id = clean($_GET['edit']);
$cond = "id = '$id'";
if($Admin->save_advance($table,$_POST,'',$cond))
{
$_SESSION['message'] = "News Updated Successfully";
header("Location:add_news.php");
exit;
}
}
}
?>
What am I doing wrong?
Tell me if I'm wrong but this line
<option value="<?php if(isset($_GET['edit'])){ echo $data['category_name']; }?>">
<?php if(isset($_GET['edit'])){echo $data['category_name'];}?>
</option>
seems strange to me, it sets the category name as value for first option in the select. Basically, if you don't change the listbox, you are trying to update a row where the category_id is a category_name.
This should be:
<option value="0">
<?php if(isset($_GET['edit'])){echo $data['category_name'];}?>
</option>
But if you have to update something you can't do that without a value. So add some kind of validation that forbids to click the save button without setting a value in the select, or just do something like this (adds a default category to the listbox)
<select name="cat_id" class="select" required>
<?php
$counter = 0;
foreach($cat as $load_category){ ?>
<option <?php echo ($counter == 0) ? 'selected' : ''; ?> value="<?php echo $load_category["category_id"]; ?>"><?php echo $load_category["category_name"]; ?></option>
<?php
//increments counter
$counter++; }?>
</select>

How to show selected value of dropdown list from database in php

How do I show the selected value of a dropdown list from my mysql database. The dropdown list is dependent to my Category dropdown list. These are the codes:
<?php $id = $_GET["id"];
if(!$pupcon){
die(mysql_error());
}
mysql_select_db($database_pupcon, $pupcon);
$getDropdown2 = mysql_query("select * from tblitemname where CategoryID = $id");
while($row = mysql_fetch_array($getDropdown2)){
echo "<option value=\"".$row["ItemID"]."\">".$row["Item_Name"]."</option>";
} ?>
Here are the codes for the first dropdown list (Category) which populates the Item Name dropdown.
<select name="Category" id="Category" class="field select large" onChange="loadXMLDoc(this.value);">
<?php do { ?>
<option value="<?php echo $row_Recordset1['CategoryID']?>"<?php if (!(strcmp($row_Recordset1['CategoryID'], $row_editRecordset['CategoryID']))) {echo "selected=\"selected\"";} ?>><?php echo $row_Recordset1['CategoryName']?></option>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); $rows = mysql_num_rows($Recordset1); if($rows > 0) {
mysql_data_seek($Recordset1, 0);
$row_Recordset1 = mysql_fetch_assoc($Recordset1);}?>
</select>
While you're listing out the drop down options, you can check to see if the current ItemID matches the passed id value. If it matches, throw a selected="selected" in there. Try:
$selected = ($row['ItemID'] == $id);
echo "<option value=\"".$row["ItemID"]."\" ".($selected ? " selected=\"selected\"":"").">".$row["Item_Name"]."</option>";
EDIT
I tried to clean up the code some...not sure why you're using a do...while because $row_Recordset1 wouldn't be available on the first iteration.
<select name="Category" id="Category" class="field select large" onChange="loadXMLDoc(this.value);">
<?php
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) {
?>
<option value="<?php echo $row_Recordset1['CategoryID']; ?>"<?php if (!(strcmp($row_Recordset1['CategoryID'], $row_editRecordset['CategoryID']))) { echo " selected=\"selected\""; } ?>>
<?php echo $row_Recordset1['CategoryName']; ?>
</option>
<?php
}
?>
</select>
you can use this code inside while
$selected=$row["ItemID"]==$id ?'Selected':'';
echo "<option value=\"".$row["ItemID"]."\" {$selected} >".$row["Item_Name"]."</option>";;

Categories