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.
Related
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>';
}
so when i send the form with with the first option 'public' selected. the data is inserted. but when i try submitting the form with the other option selected, the ones in the for each loop. the data no longer is sent. i have inspected the elements. and they are outputting all of the correct values. and they are displaying properly. why arent they inserting into the db? when i click submit nothing happens. but when i click submit for the first option, it works fine?
<form method='POST' action='add.php'>
<select>
<option name="user_page_id" value="<?php echo $_SESSION['user_id']; ?>">Public</option>
<?php
$dis=show_groups_select_list($_SESSION['user_id']);
foreach($dis as $key=>$list){
echo '<option name="user_page_id" value="'.$list['id'].'">'.$list['username'].'</option>';
}
?>
</select>
</form>
You need to put the name attribute on the select tag, not the option tag.
<select name="user_page_id">
<?php foreach ($dis as $key => $list): ?>
<option value="...">...</option>
<?php endforeach; ?>
</select>
I know you got your answer, just some more information for those who face the same problem but that solution did not work:
I had the same problem and it turned to be made by Bootstrap!
In case you are using Bootstrap , you need to provide class attribute of select:
<select name="any_name" class="form-control">
<option value="this is what would be sent if selected"> sth </option>
</select>
for more information take a glance at this discussion too!
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.
The select tag is being generated from the database through PHP echo command.
After I post the form including my select element, I want to be able to retrieve the content (between option tag) as well as the value in the option tag. So if I chose Volvo and submitted the form. I want to retrieve Volvo and 1.
<select name='car'>
<option value='1'>Volvo</option>
<option value='2>Saab</option>
<option value='3>Mercede</option>
<option value='4>Audi</option>
</select>
This will only retrieve the value.
$_POST['car']
how can I retrieve both?
You can't. But you know that 1 is always Volvo.
Just keep it in an array:
$cars = array('Volvo', 'Saab,' 'Mercede', 'Audi');
$car_name = $cars[$_POST['car']];
This way you can also generate the select from the array:
<select name='car'>
<? foreach ($cars as $key => $car_name): ?>
<option value='<?= $key ?>'><?= $car_name ?></option>
<? endforeach; ?>
</select>
you can get like this
<select name='car'>
<option value='1,Volvo'>Volvo</option>
</select>
and
$value = explode(",",$_POST['car']);
you get
$value[0]= 1
$value[1]= Volvo
Unless you're using jQuery to manipulate your submission, you can just modify the value attribute to include the same word as the content of the option. You wouldn't be able to submit POST data extracted from the content of the option, though.
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.