Post Values from a Multiple Select

Post values from a multiple select

You need to add a name attribute.

Since this is a multiple select, at the HTTP level, the client just sends multiple name/value pairs with the same name, you can observe this yourself if you use a form with method="GET": someurl?something=1&something=2&something=3.

In the case of PHP, Ruby, and some other library/frameworks out there, you would need to add square braces ([]) at the end of the name. The frameworks will parse that string and wil present it in some easy to use format, like an array.

Apart from manually parsing the request there's no language/framework/library-agnostic way of accessing multiple values, because they all have different APIs

For PHP you can use:

<select name="something[]" id="inscompSelected" multiple="multiple" class="lstSelected">

Getting All $_POST From Multiple Select Value

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

How to get all selected values from multiple select option?

Use name as like problems[] instead of problems

//Simple Form and getting the values

<form name="s" method="post" action="" >
<select multiple="multiple" name='problems[]' id='problems' class="inpBox multiple" size='50' style="height:150px;" >
<option value="Cannot Copy">Cannot Copy</option>
<option value="Cannot Print">Cannot Print</option>
<option value="Cannot Print">Cannot Scan</option>
<option value="Cannot Fax">Cannot Fax</option>
<option value="Lines Appear When Printing/Copying">Lines Appear When Printing/Copying</option>
<option value="Scan to Email Failed">Scan to Email Failed</option>
<option value="Toner Low/Empty">Toner Low/Empty</option>
<option value="Others">Others</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>

<?php
if (isset($_POST['submit'])) {
$problems = implode(',', $_POST['problems']);
echo $problems;
}
?>

Retain select and multiple select array values on form POST

Inline you would do something like

// for 'age' (single select)
<option value="0"<?php if(isset($_POST['age']) && $_POST['age']==0) echo ' selected'; ?> >Under 50</option>

//for 'fruit' (multiple select)
<option value="Apple"<?php if(isset($_POST['fruit']) && in_array('Apple',$_POST['fruit'])) echo ' selected'; ?>>Apple</option>

You would need to do this for each select <option>

If you want to do this is a php loop you could do

// for 'age' (single select)
<select id="age" name="age">
<option value=""></option>
<?php
$opts = array(0=>'Under 50',1=>'50+');
foreach($opts as $k=>$v) {
$s = (isset($_POST['age']) && $_POST['age']==0) ? ' selected': '';
echo "<option value=\"{$k}\"{$s}>{$v}</option>";
}
?>
</select>

//for 'fruit' (multiple select)
<select id="fruit" name="fruit[]" multiple="multiple">
<?php
$opts = array('Apple','Banana','Orange');
foreach($opts as $v) {
$s = (isset($_POST['fruit']) && in_array($v,$_POST['fruit'])) ? ' selected': '';
echo "<option value=\"{$v}\"{$s}>{$v}</option>";
}
?>
</select>

Get Multiple name value of multiple select in PHP POST

It is not very clear what you are trying to obtain. My best guess is that you want to link the first mailwizz_domain text input to the first redirect_url_variables select and the second mailwizz_domain to the second redirect_url_variables.

This is not happening because redirect_url_variables collects the values from both selects in a single array (that may even be empty if no option is selected).

So, redirect_url_variables should be a bidimensional array, where the first index is linked to the relative mailwizz_domain index.

For example:

<!-- First input -->
<input type="text" name="mailwizz_domain[1]">
...
<select multiple="multiple" name="redirect_url_variables[1][]">...
...

...
<!-- Second input -->
<input type="text" name="mailwizz_domain[2]">
...
<select multiple="multiple" name="redirect_url_variables[2][]">...
...

How to get multiple selected values of select box in php?

If you want PHP to treat $_GET['select2'] as an array of options just add square brackets to the name of the select element like this: <select name="select2[]" multiple …

Then you can acces the array in your PHP script

<?php
header("Content-Type: text/plain");

foreach ($_GET['select2'] as $selectedOption)
echo $selectedOption."\n";

$_GET may be substituted by $_POST depending on the <form method="…" value.

Post multiple select

Since $val['cate'] is an array, you need to transform it in a string. The fastest way to do that with your code is:

$category = implode(', ', $val['cate']);

Saving all values in multiple select

Often overlooked, super simple.

The name attribute needs to allow for multiple selections to be sent over $_POST as an array. For example:

<select name="my_meta_box_select[]" id="my_meta_box_select" multiple="" style="width:300px; height:400px;">
<option value="red">Red
</option>
<option value="blue">Blue
</option>
</select>

Notice the [] in the name: name="my_meta_box_select[]"

This, alongside the multiple attribute, will allow your $_POST variable to contain all selections as an array. That said, $_POST['my_meta_box_select'] will not just be a simple value, but rather will be an array will all selections.

How to post multiselect selected options as one param in url - Bootstrap Multiselect

I got it done by using a hidden field.

var text = $('#sites').val();
$('#sites_hidded').val(text);

with html (removed name attr from select so it will not submit with form)

<select id="sites" class="form-control" multiple="multiple">
<option value="1">Site 1</option>
<option value="2">Site 2</option>
<option value="3">Site 3</option>
</select>
<input type="hidden" name="sites" id="sites_hidden">

multiple select POST issues

If you want every option in the selectedAtts dropdown to be passed to the $_POST array you need to actually select them. So add the selected attribute to each option.

      <select size="5" id="selectedAtts" name="selectedAtts[]" multiple="">
<option selected value="bob">bob</option>
<option selected value="jim">jim</option>
<option selected value="frank">frank</option>
</select>

Of course, nothing (as the code currently stands) is to stop the user from deselecting any or all of the options...



Related Topics



Leave a reply



Submit