当多个提交不属于表单时如何处理

时间:2019-02-18 11:48:12

标签: php forms checkbox submit form-submit

要读取带有多个提交选项的多个复选框的值,请使用以下代码:

<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进行后期操作

2 个答案:

答案 0 :(得分:1)

您需要将“提交到”按钮更改为

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

答案 1 :(得分:0)

在这种情况下,您必须使用一些jquery:

  <script>
    $("input[type='submit']").click(function(){
     var _form = $("form");
    _form.append($("<input/>",{"name":$(this).attr("name")}));
    _form.submit();
   });
 </script>