【问题标题】:How to handle several submits when they are not part of a form当它们不属于表单时如何处理多个提交
【发布时间】:2019-07-11 19:10:26
【问题描述】:

为了读取具有多个提交选项的多个复选框的值,我使用以下代码:

<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 进行发布操作

【问题讨论】:

  • 将 type="submit" 更改为 type="button" 为每个按钮提供 id ,在单击事件时,您可以序列化表单数据并通过 ajax 发布
  • 你能给出一小段示例代码吗?我的意思是,对于点击事件和序列化

标签: php forms checkbox submit form-submit


【解决方案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)
});

【讨论】:

    【解决方案2】:

    对于这种情况,您必须使用一些 jquery :

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

    【讨论】:

      猜你喜欢
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多