I have a custom form and am trying to store multiple values in the usermeta. This is a sample code I am using, however it just stores the last option selected (i.e. if I select from Volvo to Opel, it will only store Opel in the database), whereas what I am wanting is all the values selected to be stored. Im not sure what Im doing wrong.
Any help would be appreciated.
<select name="test" id="test" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
update_usermeta( $new_user, 'test', $_POST['test'] );
according to the wordpress codex arrays and objects are automatically serialized
http://codex.wordpress.org/Function_Reference/update_user_meta
so you shouldn't have any problems with it. if you need to grab multiple values from the select you will need to treat it as an array name="test[]" like this
<select name="test[]" id="test" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Related
I have this HTML code :
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
and to make user able to select more than one items, I'm using this jQuery plugin : http://harvesthq.github.com/chosen/
but, once it submitted... the PHP script only able to retrieve one of $_POST['cars'] value. the last one. how to make PHP to be able to retrieve ALL of it?
I've found the answer...
<select name="cars[]" multiple="multiple">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
and in the PHP part :
$cars = $_POST['cars'];
print_r ($cars);
you must do the following:
//$_POST or $_GET is the method of your form request
foreach ($_POST['cars'] as $selected_option) {
echo $selected_option;
}
that's it.
I have a form with a dropdown list using <select> and <option> tags.
below that I have a textarea field.
The option ID's are the databases names, and I need to insert the textarea-input into the selected database.
Code:
HTML FORM:
<select class="form-control" id="selection" name="selection_name">
<option id="option1">First item</option>
<option id="option2">Second item</option>
</select>
PHP:
<?php
$dbToInsert = $_POST[''] // <-What do I insert here to get the selected option?
...
?>
You Should use "value" in option tag
<select name="vehicle">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
In the above example, if you have chosen "Audi", on form submission you will get the value "audi" in $_POST["vehicle"]
My Multiselect input code is
<?php echo $this->Form->input('Dispensary.role.',array('options'=>$dispensary_users,'class'=>'form-control dispensary_users_dd','label'=>false,'style'=>'width:300px;','empty'=>'Select Users'));
?>
and generated html code :
<select name="data[Dispensary][role][]" class="form-control dispensary_users_dd" style="width: 300px; display: none;" id="DispensaryRole">
<option value="">Select Users</option>
<option value="9">Yashobanta</option>
<option value="80">Yash</option>
<option value="83">Ramesh</option>
</select>
But when I select all users, and print_r($this->data);
returns last selected input.
But I want all.
I forgot to add multiple => true.
I don't know how that thing with $this->Form->input(...); works, because it's not important. You can select only one opinion without multiple attribute.
Example by w3schools.org:
<select name="cars" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
For saving the multiple select value in database, store the array using implode function.
In controller:
if (!empty($this->data)) {
$this->data['Category']['name'] = implode(",",$this->data['Category']['name']);
$this->Category->create();
$this->Category->save($this->data);
}
Hope it's help :)
I have an option panel in which the selected name id is set in the database. i would like to select multiple options and set more name id's then one.
The code for the option panel is like this:
<select name="werknemer[]" data-placeholder="Werknemers..." class="chzn-select" id="werknemer" tabindex="4">
<option value=""></option>
<?if($werknemers !=null):foreach($werknemers as $row):?>
<option value='<?=$row->idWerknemer;?>'><?=$row->Voornaam;?> (<?=$row->Achternaam;?>)</option>
<?endforeach;endif;?>
</select><br /></div></div>
this works perfect but i would like to select multiple "idWerknemer" in one option value.
How do i make this work?
i have tried multiple style. but that doesnt post anything.
<select multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Your code will become
<select name="werknemer[]" multiple data-placeholder="Werknemers..." class="chzn-select" id="werknemer" tabindex="4">
<option value=""></option>
<?if($werknemers !=null):foreach($werknemers as $row):?>
<option value='<?=$row->idWerknemer;?>'><?=$row->Voornaam;?> (<?=$row->Achternaam;?>)</option>
<?endforeach;endif;?>
</select>
Keyword is "multiple".
see : http://www.w3schools.com/tags/att_select_multiple.asp
After selecting few values in selectoption, post your form.
You $_POST will look like this:
Array
(
[werknemer] => Array
(
[0] => 1
[1] => 2
)
)
See example of checkbox : Multiple checkbox options on a PHP form
Use multiple attribute
<select multiple="multiple">
A select list in a form, I know there's no problem to pass the option value using post or get. But if I also want to pass the select id value, is it possible and how?
For example:
<form action="Test.php" method="POST">
<select name="select" id = '1'>
<option value="">Select a Status</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" value="submit"/>
</form>
And here is the simple print php code:
<?php
print_r($_POST['select']);
?>
It can print the value of the option that has been chosen, but how to modify the code if I also want to post the id value, which is 1 here. I want to do so because I want the id to become a variable to store some int numbers. Thank you!
The id attribute is designed purely for use in client side code. If you want to pass extra data, then use hidden inputs — that is what they are designed for.
<input type="hidden" name="select_extra" value="1">
<select name="select">
<option value="">Select a Status</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
ID's won't get passed automatically in HTTP.
You need either:
Use an input hidden
Use a special name. I.E. <select name="select_1" id="1">...</select