I have edit form where I get info from database
<select name="table">
<?php
//fetch all tables from database
$user = $con->query("SELECT * FROM table") or die(mysql_error());
while($row = $table->fetch_object()) { ?>
<option value="<?php echo $row->tablename;?>">
<?php echo $row->tablename; ?>
</option> <?php }?> </select>
<label for="time">Time :</label>
<select name="time">
<option value="twotothree">2PM-3PM</option>
<option value="threetofour">3PM-4PM</option>
<option value="fourtofive">4PM-5PM</option>
<option value="fivetosix">5PM-6PM</option>
</select>
The names of the tables from drop-down menu are different. Everytime I select a table and a time slot and save the data, the selection goes back to the first row of the menu. Eg I select table 3 and 4PM-5PM after saving it goes back to table 1 and 2PM-3PM. I need to be fixed on the last selection as I might use 4PM-5PM for table 4 also. Any idea? Thanks
You can add selected attribute when rendering your select list, depending on $_POST variable, when it's available. For example for you table select element:
<select name="table">
<?php
$user = $con->query("SELECT * FROM table") or die(mysql_error());
while($row = $table->fetch_object()) { ?>
<option value="<?php echo $row->tablename;?>" <?php if (isset($_POST['table']) && $_POST['table'] == $row->tablename) echo 'selected'; ?> >
<?php echo $row->tablename; ?>
</option>
<?php }?>
</select>
In similar way you can do for time select element.
Related
I'm having a select option in which values are populating from the database.
During updation I need to get that value selected.How can I do that?Please help.
Here is my code for select tag
<select name="supplier_id" class="form-control form-control-sm" id="colFormLabelSm">
<option value="0" selected="selected" disabled="disabled">
Select supplier
</option>
<?php
$supp_select = "SELECT supplier_id,supplier_name FROM supplier_details";
$supp_query = mysqli_query($con, $supp_select);
while ($supp_data = mysqli_fetch_assoc($supp_query)) {
?>
<option value="<?php echo $supp_data['supplier_id'] ?>">
<?php echo $supp_data['supplier_name']; ?>
</option>
<?php
}
?>
</select>
Put selected attribute on the option which is to be selected..Using if statement in option tag, check for which value is matching according to the value passed
E.g
<option <?php if(... ) echo "selected"; ?> value="value of db">
enter image description hereI am using a dropdown list to show values in data tables. it's working but after press search button the dropdwon list clears.
<select name="staff" required>
<option value="">Select Staff</option>
<?php
$query_subject = mysql_query("SELECT * from staffdet");
while($row_subject = mysql_fetch_array($query_subject)) {
?>
<option value="<?php echo $row_subject['staff_code'];?>"><?php echo $row_subject['staff_code']."-".$row_subject['staff_name'];?></option>
<?php } ?>
</select>
How to keep selected dropdown list values?
IF you are submitting a form on same page with POST method then below code will solve your problem
<select name="staff" required>
<option value="">Select Staff</option>
<?php
$query_subject = mysql_query("SELECT * from staffdet");
while($row_subject = mysql_fetch_array($query_subject)) {
?>
<option value="<?php echo $row_subject['staff_code'];?>" <?php if(!empty($_POST['staff']) && $_POST['staff']==$row_subject['staff_code']){echo 'selected="selected"';}?>>
<?php echo $row_subject['staff_code']."-".$row_subject['staff_name'];?>
</option>
<?php } ?>
Each row has a select dropdown, so if in the 1st row dropdown, a name has been selected and saved to the database, next time the 1st row's dropdown will show respective name based on db record.
For the 2nd, 3rd and 4th row's dropdown will show a select dropdown without any name being selected
Now i can only work out how to do it by hardcoding.
But as i said the no of student name is unknown as it can be added or deleted.
Is there anyway i can select the student name that has been saved in the db previously
<td><select class="input" name="name[]" >
<option value="select" <?php if ($row['name'] == 'select') echo
'selected="selected"'; ?> > select</option>
<option value="<?php echo $_SESSION['name'] ?>" <?php if ($row['name'] ==
$_SESSION['name']) echo 'selected="selected"'; ?> > <?php echo
$_SESSION['name'] ?> </option>
</select></td>
Your while loop is going to create a new list for each row returned. Place it inside the select tag as such
<select name="course">
<option value="0">Pick student</option>
<?php
while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['name'].'">'.$row['name'].'</option>';
}
?>
</select>
Having multiple options selected is just going to select the first option anyway. By default, the most recent addition should be the last row in the table.
Hi I have a stored value in mysql from a select box. When i call a edit page I would like the select box to populate with the value i have stored in mysql
$taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); This returns 1
<select id="taskCompany" required name="taskCompany" class="form-control">
<option value="" disabled selected>Select a Company</option>
<option value="1">building</option>
<option value="2">maintenace</option>
</select></div>
I would like the select box to show the correct option when loading the edit page. I do not store any select option in the database values or text just the value that is select when job was created.
Thanks Jon
I'd I have understood correctly, you have he value/values in your MySQL database and you want them to be shown in your drop down list:
You could do it like:
<option value = "1" <?php if($taskCompany == 1) echo "select='selected'";?>>building</option>
Or if you have the names coming from the database too.
<select id="taskCompany" required name="taskCompany" class="form-control">
<option>SELECT</option>
<?php
$res = musql_query("SELECT * FROM yourtablename");
while($row = mysql_fetch_array($res))
{
$taskCompany = $row['taskCompany'];
$co_name = $row['taskCompanyName'];
?>
<option value = "<?php echo $taskCompany; ?>" ><?php echo co_name; ?></option>
<?php } ?>
Here I have used $co_name to be displayed as a name assuming that you have building, maintenance etc stored in your database.
This is for the time being, as I wanted to demonstrate but Please
consider mysqli/PDO as mysql_* is deprecated.
<?php $taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); ?>
<select id="taskCompany" required name="taskCompany" class="form-control">
<option value="" disabled selected>Select a Company</option>
<option value="1" <?php if($taskCompany == 1) echo "selected='selected'"; ?>>building</option>
<option value="2" <?php if($taskCompany == 2) echo "selected='selected'"; ?>>maintenace</option>
</select></div>
You can do this
<?php foreach($taskCompany as $row){ ?>
<option value="unique id for select options">name for select options</option>
<?php } ?>
I want to filter the options to display in the second dropdown list ('make') based on selected id from previous dropdown list ('type') instead of displaying all 'make' options.
How do I capture the id of type(first dropdown) in order to display list in make accordingly before the form is submitted?
here's the code:
mysql_select_db($database);
$query="SELECT * FROM car_make";
$result= mysql_query($query)or die(mysql_error());
mysql_select_db($database);
$query_type="SELECT * FROM car_type";
$result_type= mysql_query($query_type)or die(mysql_error());
<tr><td>Choose Car Type</td><td>:</td>//first dropdown (type)
<td>
<select name="type">
<option value="">Please select a car Type</option>
<?php
while($row_type=mysql_fetch_array($result_type))
{
$carType=$row_type['carType'];
$carType_id=$row_type['carType_id'];
?>
<option value="<?php echo $carType_id; ?>"><?php echo $carType; ?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr><td>Choose Car Make</td><td>:</td>//second dropdown (make)
<td>
<select name="make">
<option value="">Please select a car make</option>
<?php
while($row=mysql_fetch_array($result))
{
$carMake_id=$row['carMake_id'];
$carMake=$row['carMake'];
?>
<option value="<?php echo $carMake_id; ?>"><?php echo $carMake; ?></option>
<?php
}
?>
</select>
</td>
</tr>
You're looking for what's called a chained select. Here's a php/ajax way to do it:
Chained Select Boxes (Country, State, City)