php 如何将多个值附加到 html 表单中的单个参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13830276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to append multiple values to a single parameter in html form?
提问by Rolando
Assuming I have the following form:
假设我有以下表格:
<form action="process.php">
<input type="checkbox" name="option1" value="oats"> Oats
<input type="checkbox" name="option2" value="beans"> Beans
<input type="hidden" name="parameter" value="a"/>
<input type="submit" value="Submit"/>
</form>
What would normally happen is after clicking on the URL, the browser redirects to:
通常会发生什么是点击 URL 后,浏览器重定向到:
process.php?option1=oats&option2=beans¶meter=a
How do I make it such that when the submit is clicked all the checkboxes end up as part of the "parameter", but separated by commas? So in other words it would be:
我如何做到这样,当点击提交时,所有复选框最终都作为“参数”的一部分,但用逗号分隔?所以换句话说就是:
process.php?parameter=a,oats,beans
Best solution with minimal javascript/jquery/html is best if no html solution.
如果没有 html 解决方案,那么使用最少的 javascript/jquery/html 的最佳解决方案是最好的。
回答by Pablo Martinez
If you want several values in one parameter, you have to name it like parameter[]
如果您想在一个参数中包含多个值,则必须将其命名为 parameter[]
f.e.:
铁:
<form action="process.php">
<input type="checkbox" name="parameter[]" value="oats"> Oats
<input type="checkbox" name="parameter[]" value="beans"> Beans
<input type="hidden" name="parameter[]" value="a"/>
<input type="submit" value="Submit"/>
</form>
回答by Artem Solovev
There is a very useful method to use for dynamic data grouping.
有一种非常有用的方法可用于动态数据分组。
<form action="file.php" method="post">
<?php for ( $i = 0; $i < count( $something ); $++ ) { ?>
<input type="checkbox" name="<?php $something[$i]; ?>[]" value="user_id"> Oats
<input type="checkbox" name="<?php $something[$i]; ?>[]" value="user_name"> Beans
<?php } ?>
<input type="submit" value="Submit"/>
</form>
回答by drchuck
I was really only interested in checkboxes that were actually checked - so I built myfunc()from the other examples like this:
我真的只对实际选中的复选框感兴趣 - 所以我myfunc()从其他例子中构建,如下所示:
function myfunc(){
$('#void input[type="checkbox"]').each(function(id,elem){
if ( ! $(this).is(':checked') ) return;
b = $("#res").val();
if(b.length > 0){
$("#res").val( b + ',' + $(this).val() );
} else {
$("#res").val( $(this).val() );
}
});
var stuff = $("#res").val();
if ( stuff.length < 1 ) {
alert('Please select at least one');
} else {
$("#real").submit();
}
}
回答by Ivan Buttinoni
More or less here the idea: first form, 1 hidden input will catch all the vaules of second and void form
或多或少这里的想法是:第一种形式,1 个隐藏输入将捕获第二种和无效形式的所有值
<script type="text/javascript">
function myfunc(){
$('#void input').each(function(id,elem){
b = $("#res").val();
if(b.length > 0){
$("#res").val( b + ',' + $(this).val() );
} else {
$("#res").val( $(this).val() );
}
});
alert("VAL "+$("#res").val());
$("#real").submit();
return false;
}
</script>
<form id="real" action="process.php">
<input id="res" type="hidden" name="parameter" value=""/>
</form>
<form id="void">
<input type="checkbox" name="option1" value="oats"> Oats
<input type="checkbox" name="option2" value="beans"> Beans
<input type="hidden" name="parameter" value="a"/>
<input type="submit" value="Submit" onClick="javascript:myfunc()"/>
</form>
add some fix, submit the right form. Tested on jsfiddle:
添加一些修复,提交正确的表单。在 jsfiddle 上测试:
ADD SINGLE FORM VERSION
添加单一表格版本
You've to know the name of the field, and ignore the other params in the answer
您必须知道该字段的名称,并忽略答案中的其他参数
<script type="text/javascript">
function myfunc(){
// can use more selector
$('#real input').each(function(id,elem){
// append the data to this field so no need to process it
if(this.id == 'res'){
return;
}
// here I concat the values
b = $("#res").val();
if(b.length > 0){
$("#res").val( b + ',' + $(this).val() );
} else {
$("#res").val( $(this).val() );
}
});
alert("VAL "+$("#res").val()); // remove me
$("#real").submit();
return false;
}
</script>
<form id='real' method='POST' action="#">
<input type="checkbox" name="option1" value="oats"> Oats
<input type="checkbox" name="option2" value="beans"> Beans
<input id="res" type="hidden" name="parameter" value="a"/>
<input type="submit" value="Submit" onClick="javascript:myfunc()"/>
</form>?

