簡體   English   中英

當多個提交不屬於表單時如何處理

[英]How to handle several submits when they are not part of a form

為了讀取帶有多個提交選項的多個復選框的值,我使用以下代碼:

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM