要使用多个提交选项读取多个复选框的值,我使用以下代码:
<form action="" method="post">
<input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
<input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
<input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><br/>
<input type="submit" name="delete" value="Delete"/>
<input type="submit" name="move" value="Move"/>
<input type="submit" name="copy" value="Copy"/>
</form>
每个提交应该做一个不同的操作,我的PHP看起来像这样:
if($_POST['delete']) {
if(isset($_POST['check_list'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
}
// code for delete goes here
echo 'Files are deleted!';
}
}
}
if($_POST['move']) {
if(isset($_POST['check_list'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
}
//code for moving files goes here
echo 'Files are moved!';
}
}
}
if($_POST['copy']) {
if(isset($_POST['check_list'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
}
// code for copy goes here
echo 'Files are copied!';
}
}
}
这对我来说很好。我想要实现的目标:我想将提交的内容放在网站上完全不同的地方。如下所示:
<form action="" method="post">
<input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
<input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
<input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><br/>
</form>
<!-- some code goes here -->
<input type="submit" name="delete" value="Delete"/>
<input type="submit" name="move" value="Move"/>
<input type="submit" name="copy" value="Copy"/>
我怎样才能做到这一点?
顺便说一句:我正在使用ajax进行后期行动
您需要将提交更改为按钮
<input type="button" class="submit-form" name="delete" value="Delete"/>
<input type="button" class="submit-form" name="move" value="Move"/>
<input type="button" class="submit-form" name="copy" value="Copy"/>
并需要添加id到表单
<form action="url/goeshere" id="my-form" method="post">
你的ajax表单提交将是这样的事情
$(".submit-form").click(function(event) {
$form = $("#my-form");
$.post($form.attr("action"), $form.serialize() + "&submit="+
$(this).attr("value"), function(data) {
// do something with response (data)
});
你必须在这种情况下使用一些jquery:
<script>
$("input[type='submit']").click(function(){
var _form = $("form");
_form.append($("<input/>",{"name":$(this).attr("name")}));
_form.submit();
});
</script>