我希望能够将表单内的组合框中的所有值发送到php $ _POST变量中。
注意:我不需要发送所选的值,但所有内容都包含多个选择。
我的尝试是这个(这不是正确的方式,但我认为它很接近):
HTML code:
<form action="" method="post" name="form" id="list2">
<select multiple size="8" style="width:280">
<option value="aline.chastel" name="user[]">Aline Chastel</option>
<option value="bruno.freitas" name="user[]">Bruno Sousa Freitas</option>
</select>
<input type="submit" name="submit" value="Send" />
</form>
PHP代码:
if(isset($_POST['submit'])){
echo $_POST['user'][0].' '.$_POST['user'][1];
}
预期成果:aline.chastel bruno.freitas
我的解决方法:
经过研究,我发现用纯HTML做不到这一点,所以我不得不使用javascript和AJAX。我把所有的子元素都加在了一个带有'|'的字符串中:
$("#btngrafico").click(function() {
for(i=0;i<$("#list2").children().length;i++){
var co_usuario = $("#list2").children('option').eq(i).attr('value');
var usuario = $("#list2").children('option').eq(i).text();
if (i==0){
arr_co_usuario = co_usuario;
}else{
arr_co_usuario = arr_co_usuario + '|' + co_usuario;
}
}
$.ajax({
url: 'GraficoBarras.php',
type:'POST',
data: { 'arr_co_usuario': arr_co_usuario,'Month1':Month1,'Year1':Year1,'Month2':Month2,'Year2':Year2},
beforeSend: function(){
$("#loader").show();
},
success: function(output_string){
$("#ListadoRelatorio").append(output_string);
}
});
});
答案 0 :(得分:1)
您的name
属性应位于select标记中,例如:
<select multiple size="8" style="width:280" name="user[]">
<option value="aline.chastel">Aline Chastel</option>
<option value="bruno.freitas">Bruno Sousa Freitas</option>
</select>
在表单提交上查看PHP端的数据,执行:
print_r($_POST["user"]);
if (isset($_POST['user'])) {
foreach ($_POST['user'] as $name) {
echo $name."<br />";
}
}
答案 1 :(得分:1)
为此必须使用多选列表,因为选择列表只发送一个选中的值。
如果您只想使用此选择列表,那么我认为您只能通过jquery获得所有选项