pre-selected values for a dynamic drop-down box - php

Here is my problem, I have dropdown which used to save the selected option to a SQL database. Now I have an Edit option where the same dropdown is created dynamically to give the user to select and alternate option and save the edit. When the edit page launches I want the dropdown to be pre-selected the value already saved in the database.
I use following code to get done the similar thing with a textbox but strugling to put the same value attribute to the dropdown.
<input name='routename' type='text' value='".htmlentities($row['route'])."'> // This is working for the textbox
can someone tell me how to do this with a dropdown box? Thanks

It's a little more complicated but can be achieved with this:
<select name="something">
<option value="1"<?=($row['something'] == 1)? ' selected="selected"':''?>>Option 1</option>
<option value="2"<?=($row['something'] == 2)? ' selected="selected"':''?>>Option 2</option>
</select>

You will have to use conditional code as you generate your <option> nodes like this:
<?php
echo '<option ';
if ($value == $selected_value)
echo 'selected="selected"';
echo 'value="'.htmlspecialchars($value).'" />';
?>

while($row=mysql_fetch_row($rs)){
if($row['id']==$value){
$selected='selected';
}else{
$selected='';
}
echo '<option value="$row['id']" $selected >$row['value']</option>';
}

Related

Presetting a select element

I have a form with several input fields plus a select element that has several dropdown options. The user fills in the input fields and selects one of the drop down options and submits the form. The php handler for the form saves the form inputs in a database and builds a page from these inputs. An edit link on the built page returns to the form page with the fields preset to what they were the last time the form was submitted, so the editing can start from the last state submitted.
Presetting the input fields is easy, where I can just specify value=<?php echo [saved value from last submission];?> But presetting the select element to the last value chosen is not so easy. I know that jQuery has $('select#id').val("4"); which will preset the select#id element to option #4, but how can I trigger this when the form page is loaded again? Presettng the inputs by setting value= doesn't seem to generate anything that can be sensed by jQuery.
Thanks for any ideas.
Set the attribute selected for one of the options you want to be the selected. So, something like <option <?php echo 'selected="selected"';?>>4</option>.
Edit:
Either you can generate the options in a loop having the values in an array, checking each iteration if the current option was the one that was selected by the user and set the selected attribute of that one option, or you can just use the following, although not so elegant, approach:
<select name="number">
<option value="1" <?php echo ($_POST['number'] == '1') ? 'selected="selected"' : ''; ?>>1</option>
<option value="2" <?php echo ($_POST['number'] == '2') ? 'selected="selected"' : ''; ?>>2</option>
<option value="3" <?php echo ($_POST['number'] == '3') ? 'selected="selected"' : ''; ?>>3</option>
<option value="4" <?php echo ($_POST['number'] == '4') ? 'selected="selected"' : ''; ?>>4</option>
</select>
user2570380:
I think your edited solution would work so I gave you credit. But I've found another way that works that may be even more elegant. In the HTML of my PHP file I've placed at the bottom:
<script>
jQuery('select#age_group').val(<?php echo $age_group?>);
</script>
</body>
Here select#age_group is the select element I want to preset and $age_group is the value coming into PHP that I want to set into the select element.

I have trouble in set_select in codeigniter

i have worked in a codeigniter framework and i have a problem in select tag.
when i want to achieve the value of option that i have selected.
i used the method as when i select the option value then i pressed the search button and after what i need the result its showing and then i want to again the value of the selected option with second submit button and the whole data with selected option value must be go to data base. the code is as follow.
<select id="faculty" data-rel="chosen" name='stdid'>
<option>select student</option>
<?php
foreach ($student as $s) {
echo '<option set_select("student","'.$s['stdid'].'","true") value="'.$s['stdid'].'">'.$s['stdname'].' ' .$s['stdid'].'</option>';
}?>
</select>
You are confusing PHP with HTML. Change the code to this:
<select id="faculty" data-rel="chosen" name="stdid">
<option>select student</option>
<?php
foreach ($student as $s) {
echo '<option'.set_select("stdid", $s['stdid'], true).' value="'.$s['stdid'].'">'.$s['stdname'].' '.$s['stdid'].'</option>';
}?>
</select>
Note that setting all the option elements to set_select("stdid", $s['stdid'], true) will become selected="selected". Read more about set_select.

How can I retain a option from a select field?

I have a <select>:
<select id="countries">
<option value="1">USA</option>
<option value="2">Spain</option>
</select>
The user selects an option, then he press a send button to make a query, thru PHP. The result of the query appears in the same page, so the page reloads.
How do i retain the selected option? when the page reloads??
I mean, if they select spain, how can I see spain again when the page reloads?
You need to first give your select dropdown a name.
For example:
<select id="countries" name="countries">
Then you will have access to the value in your PHP when the form is submitted. The value can be retrieved like this in PHP (after submit):
$countries = $_POST['countries'];
Then you can do something like #JohnConde did by setting the selected attribute with PHP.
Here's a very basic way to do it:
<option value="1"<?php if (1 === (int) $_POST['countries']) echo ' selected="selected"'; ?>>USA</option>
<option value="2"<?php if (2 === (int) $_POST['countries']) echo ' selected="selected"'; ?>>Spain</option>

select dropdown value based on session variable

i have a php form which submits data to another php for validation. if any fields are left blank the validator php sends a message back to original php form along with all the pre filled variables using session variables so that user doesn't have to fill them again.
i can use these session variables to fill in text boxes again like this
value="<?php echo $_SESSION['fname'] ?>"
but how to go about populating drop downs, radio buttons and check boxes?
thanks
For a select, checkbox or radio box you would need to check whether the values match. Something like:
<select name="fname">
<option value="1" <?php if($_SESSION['fname'] == 1) echo 'selected'; ?>>1</option>
<option value="2" <?php if($_SESSION['fname'] == 2) echo 'selected'; ?>>2</option>
</select>
It's easier if you are iterating through the values for the option field:
<select name="fname">
<?php foreach($array AS $key => $value) { ?>
<option value="<?php echo $value; ?>" <?php if($_SESSION['fname'] == $value) echo 'selected'; ?>><?php echo $value; ?></option>
<?php } ?>
</select>
A <select> element's selected <option> is not determined by it's value= attribute. In order to mark an option selected, the <option> element must have the selected attribute set on it, as follows:
<select>
<option>1</option>
<option selected>2</option>
<option>3</option>
</select>
In order to do this programmatically, you need to iterate through the options, and manually test for the correct option, and add selected to that.
... Noticed that your double quote at the end of the statement was misplaced, it should be:
<script>
$("#fname").val("<?php echo $_SESSION['fname'] ?>");
</script>
There is a far simpler method using JQuery. You can set Selects with .val() so make the most of it.
<script type="text/javascript">
$("#fname").val("<?php echo $_SESSION['fname']; ?>");
</script>
Don't forget to set the id of the select as well as the name to fname.

two select at a time in combo box - how to solve it?

Here I'm using combo box for searching working fine fetch from MySQL.
here is the code
<select name="location" class="styled">
<option selected="selected" value=''>Any</option>
<?php
while ($loc_row = mysql_fetch_array($loc_result))
{
echo "<option value='$loc_row[job_location]'";
if($loc_row['job_location'] == $location)
{
echo 'selected="selected"';
}
echo ">$loc_row[job_location] </option>";
}
?>
</select>
when click on search get string becoming as selected $location=isset($_GET['location'])?$_GET['location']:''; during this time there are two selected value.
could any one can help me to be one select value please
Remove the selected="selected" from the default "Any" option. The first element is selected by default.

Categories