This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this questionI am trying to submit this multi select as an array but when I submit it nothing prints to the screen so what am I doing wrong?
HTML
<div class="container">
<select name="itemsToChoose" id="left" size="8" multiple="multiple">
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
<option value="4">item4</option>
<option value="5">item5</option>
</select>
</div>
<div class="low container">
<input name="left2right" value="add" type="button">
<input name="right2left" value="remove" type="button">
</div>
<form Method ="POST" action="multiSelectRead.php">
<div class="container">
<select name="itemsToAdd" id="right" size="8" multiple="multiple">
</select>
</div>
<input type="submit" style="width: 75px; border: 1px solid gray" value="Submit">
<br />
</form>
The JS code:
$(function() {
$(".low input[type='button']").click(function() {
var arr = $(this).attr("name").split("2");
var from = arr[0];
var to = arr[1];
$("#" + from + " option:sel开发者_运维知识库ected").each(function() {
$("#" + to).append($(this).clone());
$(this).remove();
});
});
})
$('form').submit(function() {
$('#seldist option').each(function(i) {
$(this).attr("selected", "selected");
});
});
multiSelectRead.php
<?php
print_r($_POST["itemsToAdd"]);
?>
The user is suppose to add values then hit submit. But I tried it and yet it wont pass the itemstoAdd values in array to php....
Here is snippet: http://jsfiddle.net/an4gA/
In the jsFiddle you posted, your jQuery code to select the options reads:
$('#seldist option').each(function(i) {
$(this).attr("selected", "selected");
});
Thing is, your select box's ID is "right".
Either change the #seldist
above to #right
, or change your select box's ID to "seldist".
There is nothing in your select. No values at all.
All values in a select in your code snippet are in itemsToChoose
and that one is not within your <form>
tags.
Do a print_r($_POST);
to see what is submitted and try again.
Also try moving the <form>
starting tag to include the itemsToChoose
.
edit: As Michael pointed out, you should make it an array by adding []
to the select name
You must make the form element's name into an array with []
:
<select name="itemsToChoose[]" id="left" size="8" multiple="multiple">
<select name="itemsToAdd[]" id="right" size="8" multiple="multiple">
Secondly, in your form you use itemsToChoose
, which has options, and in your PHP you call itemsToAdd
which has no options to select.
UPDATE
I see an errant space in the form's method
attribute. This is causing an empty $_POST
, but probably not an empty $_REQUEST
or $_GET
.
<form Method ="POST" action="multiSelectRead.php">
^^^^
<form method="POST" action="multiSelectRead.php">
精彩评论