How can get the circled valued with PHP?
How can get a value from drop down tag of HTML with PHP? Not the value of value tag.
The data of the label in the option field is not passed to the server, so you cannot get that data without modifying the HTML too.
You could change the form simply though:
<option value="first">first</option>
<option value="second">second</option>
etc.
Put the values and texts in an array. Then retrieve the needed text by using the value of the chosen option as array key.
Try:
$q_array = array(
'1'=>'First',
'2'=>'Second',
'3'=>'Third',
'4'=>'Fouth',
'5'=>'Fifth'
);
echo $q_array[$_POST['quantity']];
Related
I'm using the select2 multiple dropdown tags select plugin. On form submit i've got it putting the info into my database.
When just using $this->input->post('sellingmethods') it only inputs the last option selected.
I want to put them into my database like option1,option2,option3
so i tried using implode
'sellingmethods' => implode(',', $_POST['sellingmethods']),
however I keep being thrown the error :
Message: implode(): Invalid arguments passed
any ideas?
You are getting an error trying to use $_POST['sellingmethods'] as an array, which means that it's not an array. This makes me think you have not named your control correctly. Look at this example and notice the brackets in the name and the use of the multiple attribute.
<select name="sellingmethods[]" multiple="multiple">
<option value="1">method 1</option>
<option value="2">method 2</option>
...
</select>
I populate my select from an array, in the loop I echo lines like this
echo "<option value='".$company->id."' selected>".$company->name."</option>";
when this compiles it compiles like this:
<option selected="" value="1">Company</option>
Also when I try to retrieve the value of <select name='companies'>
with line like this $_POST['companies'] I get nothing back.
Thanks
If your form method is post then you will get value with $_POST['companies']
If you are using selected for each option then please remove that because in that you will get value of always last option
I have selectbox option list with multiple selection.
<select multiple="MULTIPLE" size="20" name="facilities[]">
<option id="idOne" value="Value One">Value One</option>
<option id="idTwo" value="Value Two">Value Two</option>
<option id="idThree" value="Value Three">Value Three</option>
</select>
I have done already the php code that can store the value into an Array on my database, but.. what I want now, is that possible to use the ID from the option as a key of the array, so when you storing the array will look like sample below
array('idOne'=>'Value One', 'idTwo'=>Value Two, 'idThree'=>'Value Three')
Instead of array([0]=>'Value One', [1]=>Value Two, [2]=>'Value Three') (that what I get now).
Any kind help will much appreciate :)
Sorry my English bad.
By definition, only the value of your option tags will be passed in your POST. There's a couple of options here
First, you would need to know the values on the other end and marry them up. This is more work potentially but you would always have a list of the values selected.
$vals = array('idOne'=>'Value One', 'idTwo'=>'Value Two', 'idThree'=>'Value Three');
foreach($vals as $key => $row) {
if(array_search($row, $_POST['facilities']) === false) unset($vals[$key]);
}
Second would be to use JavaScript and build a list of the text values that are selected and POST that (either via hidden field or using AJAX). This is riskier since the data would be in the client's hands but still doable.
This is my drop down list.
<p align="center"><select size="1" name="bo_chose" id="boID">
<option selected value="Select...">Select...</option>
<?php
while ($list_bo = mysql_fetch_array($select_brof)) {
echo "<option value=\"$list_bo[bo_name] $list_bo[bo_code]\">$list_bo[bo_code],$list_bo[bo_name]</option>"; }
?>
</select></p>
So the drop down will show first "select..."
and then will retrieve data andlist the bo_name, bo_code in <option>
It works well.
The problem is, I want to carry the value to another PHP page which will delete the
selected option in the drop down.
Of course the MySQL and PHP will complain that it does not exist ...why?
Its taking the new value $bo_chose (name of the dropdown list) as a new value
as (bo_name, bo_code) — as one value not as a split values.
So if the dropdown list is (george Mike GM)
the data will complain that there is no value called "George Mike GM"
when I want it to carry only "GM" which is the bo_code.
Can anyone help?
Just split the value with space as delimiter and pick the first value. May I know why do you
need to carry both name and id in first place.
You should just add $list_bo['bo_code'] as option value instead of both.
If I understand you correctly you have this select inside a POST form or you are sending it by ajax to the script you want to parse the value. Use a separator to merge these two variables. The separator should be a char that you are sure, it will never be in either of two variables. So, you can for example use ; for the separator and write $list_bo[bo_name].';'.$list_bo[bo_code]. In the recieving script you just explode the value by separator and you will get both values as array. If you are unsure which chars will be in both variables, you can either json_encode or serialize it before puting it into html.
I have hard coded and added items to dropdownlist ie teamsize as 1,2,3 like that.
When i load this dropdownlist for edit/update i get duplicate values like this
1
1
2
3
4... How do i eliminate this duplicate values?
please find the code below
<select name="anesthesia" id="selectAnesthesiaVal" style="width:25%;" class="required safe" AppendDataBoundItems = "false">
<option value="<?php echo isset($event)?$event->proc_anesthesia_type:"" ;?>"><?php echo isset($event)?$event->proc_anesthesia_type:"" ;?></option><option value="General">General</option>
<option value="Mac">Mac</option>
<option value="Spinal/Epidural">Spinal/Epidural</option>
<option value="Regional">Regional</option>
<option value="Local">Local</option>
<option value="Other">Other</option>
</select>
If you do not want to use jQuery for this then I would advise placing all of the possible values into an array and looping through them in PHP. Then if the value exists, only place it once.
In addition, if you would like to use jQuery and the PHP is not possible in your circumstances then let me know and I will post up some jQuery.
UPDATE
This will do the trick. I have clearly laid out comments to explain what is going on step by step. Hope this helps.
Please note that it would be much more efficient to do this in PHP
// Set the present object
var present = {};
$('#selectAnesthesiaVal option').each(function(){
// Get the text of the current option
var text = $(this).text();
// Test if the text is already present in the object
if(present[text]){
// If it is then remove it
$(this).remove();
}else{
// Otherwise, place it in the object
present[text] = true;
}
});
I suspect that line
<option value="<?php echo isset($event)?$event->proc_anesthesia_type:"" ;?>"><?php echo isset($event)?$event->proc_anesthesia_type:"" ;?></option>
add option which correspond to option choosen from select control, i e on first load you see coded list as this line returns empty option, but when you choose actual option this first one gets populated with choosen value
in this case you need to do 2 things
1 remove this line
2 add conditions to each line of hardcoded option and set it to select depending on the value of $event->proc_anesthesia_type
and because of 2nd tank you will end up with 6 almost identical conditional statements putting selected='selected' to each option
so in order to make the overall code looks pretty i recomend instead of hardcodding options add values to list or even better dictionary and check this condition in a loop