CakePHP : Multiselect input data insert into database - php

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 :)

Related

Pass an array as a variable in php postgres query [duplicate]

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.

How to retrieve <option> id to insert data into database

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"]

How to store array in usermeta in wordpress

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>

select menu html value

How can I add html value for select menu options:
<select name="symbol">
<option>select</option>
<option value="<img src="start.jpg"">start</option>
<option value="<img src="end.jpg"">end</option>
</select>
I want post value to show an image.
I read this : How to add a images in select list
but I want send image value.
If you're handling the post with PHP or some similar server-side code, you should pass through only the reference to the image. Any tags can be output using the server-side code. For example:
<select name="symbol">
<option>select</option>
<option value="start.jpg">start</option>
<option value="end.jpg">end</option>
</select>
And then handle this by doing something like:
echo '<img src="'.$_POST['symbol'].'" />';
Use single quotes instead of double qoutes inside value...
<select name="symbol">
<option>select</option>
<option value="<img src='b.jpg'>">start</option>
<option value="<img src='a.jpg'>">end</option>
</select>
You have to use single quote and close tag. Try this,
<select name="symbol">
<option>select</option>
<option value="<img src='start.jpg'>">start</option>
<option value="<img src='end.jpg'>">end</option>
</select>

Working with "multiple" select

In my profile advanced search i have an mutiple select type..
<form method="POST">
<select size="6" name="Cities" multiple="multiple" id="Cities">
<option value="0">All</option>
<option value="1">City1</option>
<option value="2">city2</option>
<option value="3">city3</option>
<option value="4">city4</option>
<option value="5">city5</option>
</select>
</form>
How do I use it, I mean how should i call what they have chosed...As you can choose one or more options (by holding in control, or just holding in mouseclick and point to other options).
Give it the name Cities[] instead of Cities, and you'll have an array in $_POST['Cities'] on submit.
google helps
http://www.onlinetools.org/tricks/using_multiple_select.php

Categories