retrieving value of select after posting form - php

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.

Related

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.

Dynamically mark as selected an option in a form using HTML and PHP

I am creating a form to update some entries in a sql table.
For one of the fields, I will have a Select Option that must be "selected" in the value that the field contains.
So ideally, I would have something like this:
<select id="source" name="source">
<option <?=$manual?>>MANUAL</option>
<option <?=$etsy?>>ETSY</option>
<option <?=$online?>>ONLINE</option>
</select>
But I want to make it dynamic so, if I have 100 options, I won't have to write 100 variables, but just load the corresponding variable name with the word "selected".
Whats the best way to achieve this?
Try something like this:
<select id="source" name="source">
<?php
$Attrib = 'selected="';
if ($order['source'] === 'value'){
$Attrib.= 'selected"';
}
else{
$Attrib .= 'false"';
}
?>
<option name="" <?=$Attrib;?>>NAMEHERE</option>
Not to mention, you are assigning ($order['source']) to value, so this will always equal to "value"
May be you meant to do something like
<option <?= ($order['source']==='optionname')?'selected':'' ?> > optionname </option>
And it would be nicer to push the options to some array.
Oh, that's what u mean.
That's how i would have done it
<?php
$checkedoption = 'flytothemoon'; //the checked option
$options = array('opt1', 'opt2', 'opt3', 'flytothemoon');
foreach($options as $option):?>
<option <?= ($order['source']===$checkedoption)?'selected':'' ?> > $option </option>
<?php endforeach;?>

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.

Get the Selected value from the Drop down box in PHP

I am populating a Drop Down Box using the following code.
<select id="select_catalog">
<?php
$array_all_catalogs = $db->get_all_catalogs();
foreach($array_all_catalogs as $array_catalog){?>
<option value="<?= $array_catalog['catalog_key'] ?>"><?= array_catalog['catalog_name'] ?></option>
Now how can I get the selected option value using PHP (I have the code to get the selected item using Javascript and jQuery) because I want the selected value to perform a query in database.
Any help will be much appreciated. Thank you very much...
You need to set a name on the <select> tag like so:
<select name="select_catalog" id="select_catalog">
You can get it in php with this:
$_POST['select_catalog'];
Couldn't you just pass the a name attribute and wrap it in a form?
<form id="form" action="do_stuff.php" method="post">
<select id="select_catalog" name="select_catalog_query">
<?php <<<INSERT THE SELECT OPTION LOOP>>> ?>
</select>
</form>
And then look for $_POST['select_catalog_query'] ?
You have to give a name attribute on your <select /> element, and then use it from the $_POST or $_GET (depending on how you transmit data) arrays in PHP. Be sure to sanitize user input, though.
Posting it from my project.
<select name="parent" id="parent"><option value="0">None</option>
<?php
$select="select=selected";
$allparent=mysql_query("select * from tbl_page_content where parent='0'");
while($parent=mysql_fetch_array($allparent))
{?>
<option value="<?= $parent['id']; ?>" <?php if( $pageDetail['parent']==$parent['id'] ) { echo($select); }?>><?= $parent['name']; ?></option>
<?php
}
?></select>

Drop Down list in PHP

In my application am submiting my form by using post for a php page.
<form method="post">
<select name="txtplace">
<option value="1">ajith</option>
</select>
</form>
here when i am try to get the value of my dropdown its only getting 1.How can i get ajith
<option value="ajith">ajith</option>
Should work. You get the value as 1 because you have set the value as 1.
Maybe it's better if you define an array in php:
$arr = array ( 1 => "ajith" );
Then generate the HTML dynamically:
<option name="test" value="$i">$arr[$i]</option>
Then after post:
$key = $_GET['test'];
$value = $arr[$key];
If you just want "ajith" then do as Shoban suggests. If you need both 1 and "ajith" you could always have:
<option value="1-ajith">ajith</option>
And then with php:
$sTxtPlace = $_POST['txtplace'];
list($number,$text) = explode("-",$sTxtPlace,2);
You'll only get '1' in your form script, as that's the value of the option element. You could change to the following:
<form method="post">
<select name="txtplace">
<option value="ajith">ajith</option>
</select>
</form>
rather than using the ID (I presume) of that item.
<option value="ajith">ajith</option>
You can use a hidden element and add an onchange handler to the select to populate the label part. Advantages are that you dont have to change your rendering code and you dont need to explode.
Try:
<form method="post">
<input type="hidden" id="txtPlaceLabel" name="txtPlaceLabel" value="">
<select name="txtplace" onchange="document.getElementById('txtPlaceLabel').value=this.options[this.options.selectedIndex].innerHTML;">
<option value="ajith">ajith</option>
</select>
</form>
Note: Haven't tested the code so maybe some editing will be required.

Categories