2016-11-25 159 views
3

我試圖檢測哪個按鈕被jQuery按下,然後服務器端根據結果做不同的事情。檢測哪個按鈕被按下

jQuery工作正常(雖然我可能已經以一種習慣的方式去了)但我無法弄清楚爲什麼在我的代碼中按下任何按鈕我得到相同的響應從PHP:「添加按鈕檢測到「。我希望有人能告訴我我錯了什麼?

jQuery的

$(document).ready(function() { 
    $(".btn_add").on("click", function() { //If add btn pressed 
     var id = this.id; 
     var url = "process_ajax4.php?btn=" + this.id; 

     var formdata = $('.myForm').serialize(); 
     $.post(url, formdata, 
      function(data) { 

       $("#results").html(data); //Response 


      }); 
    }); 
    $(".btn_remove").on("click", function() { //If remove btn pressed 
     var id = this.id; 
     var url = "process_ajax4.php?btn=" + this.id; 

     var formdata = $('.myForm').serialize(); 
     $.post(url, formdata, 
      function(data) { 

       $("#results").html(data); //Response 

      }); 
    }); 

}); 

php的

<?php 
$btn=$_POST["btn"]; //Other posted variables removed for simplicity 

if($btn="btn_add"){ 

    echo "<h1>Add button detected</h1>"; 
//Do stuff 

} 
elseif($btn="btn_remove"){ 

    echo "<h1>Remove button detected</h1>"; 
//Do other stuff 
} 
?> 

HTML表單

<td> 
    <form id=\ "myForm\" class=\ "myForm\" action=\ "\" method=\ "post\" enctype=\ "multipart/form-data\"> 
     <input type=\ "hidden\" name=\ "user_id\" value=". $collab_userid." /> 
     <input type=\ "hidden\" name=\ "id\" value=".$upload_id." /> 

     <button type=\ "submit\" id=\ "btn_remove\" class=\ "btn_remove\" name=\ "btn_remove\">Remove</button> 
     <button type=\ "submit\" id=\ "btn_add\" class=\ "btn_add\" name=\ "btn_add\">Approve</button> 
    </form> 
</td> 
+0

你用'=',而不是''==比較字符串提交。 – julekgwa

回答

3

您應該添加按下按鈕,您formdata,否則點擊無法被檢測到。

$(document).ready(function() { 
    $(".btn_add").on("click", function() { //If add btn pressed 
     var id = this.id; 
     var url = "process_ajax4.php?btn=" + this.id; 

     var formdata = $('.myForm').serialize(); 
     formdata += "&btn=btn_add"; // added the btn 
     $.post(url, formdata, 
      function(data) { 

       $("#results").html(data); //Response 


      }); 
    }); 
    $(".btn_remove").on("click", function() { //If remove btn pressed 
     var id = this.id; 
     var url = "process_ajax4.php?btn=" + this.id; 

     var formdata = $('.myForm').serialize(); 
     formdata += "&btn=btn_remove"; // added the btn 
     $.post(url, formdata, 
      function(data) { 

       $("#results").html(data); //Response 

      }); 
    }); 

}); 
0

我覺得你的代碼看起來確定。

我想在php中不能比較字符串= 您可能需要將其更改爲strcmp(strA,strB)== 0以確保輸入參數是添加按鈕或刪除按鈕。

1

更改PHP代碼如下

<?php 
    $btn=$_POST["btn"]; //Other posted variables removed for simplicity 

    if ($btn=="btn_add") { 
     echo "<h1>Add button detected</h1>"; 
    //Do stuff 
    } elseif ($btn=="btn_remove"){ 
     echo "<h1>Remove button detected</h1>"; 
    //Do other stuff 
    } 


?> 
1

不必有jquery button處理兩個獨立的功能。您也可以從你的代碼,因爲你是檢測單擊事件

$(document).ready(function() { 
 
    $("button").on("click", function() { //If add btn pressed 
 
     var id = this.id; 
 
     
 
     var url = "process_ajax4.php?btn=" + this.id; 
 
     console.log(url); 
 
     var formdata = $('.myForm').serialize(); 
 
     $.post(url, formdata, 
 
      function(data) { 
 

 
       $("#results").html(data); //Response 
 

 

 
      }); 
 
    }); 
 
    }); 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<td> 
 
    <form id="myForm" class="myForm" action="\" method= "post" enctype="multipart/form-data"> 
 
     <input type="hidden" name="user_id" value=". $collab_userid." /> 
 
     <input type="hidden" name="id" value=".$upload_id." /> 
 

 
     <button type="submit" id="btn_remove" class="btn_remove" name= "btn_remove">Remove</button> 
 
     <button id="btn_add" class= "btn_add" name="btn_add">Approve</button> 
 
    </form> 
 
</td>

刪除button type="submit"可以使用parse_url()parse_str()用於獲取在PHP中的查詢字符串。爲了使用$btn=$_POST["btn"]; TBN屬性必須作爲一個表單數據傳遞,查詢參數不會將可通過這個方法

<?php 
$parts = parse_url($url); 
parse_str($parts['query'], $query); 
$btn = $query['btn']; 

if($btn=="btn_add"){ 

    echo "<h1>Add button detected</h1>"; 
//Do stuff 

} 
elseif($btn=="btn_remove"){ 

    echo "<h1>Remove button detected</h1>"; 
//Do other stuff 
} 
?> 
0

你實際上並不需要jQuery代碼在所有。由於兩個btn_removebtn_add是提交按鈕,可以查看哪個按鈕,其中用於提交表單使用:

if(isset($_POST["btn_remove"])) { 
    //Remove button was pressed. 
} 
1

你的代碼的工作只是讓var url = process_ajax4.php,這將解決您的problem.in PHP使用==代替=,還添加e.preventDefault()到您的按鈕點擊,以防止形式被用action='url'

$(document).ready(function() { 
$(".btn_add").on("click", function(e) { //If add btn pressed 
    e.preventDefault(); 
    var id = this.id; 
    var url = "process_ajax4.php"; 

    var formdata = $('.myForm').serialize(); 
    formdata += "&btn=btn_add"; // added the btn 
    $.post(url, formdata, 
     function(data) { 

      $("#results").html(data); //Response 


     }); 
}); 
$(".btn_remove").on("click", function(e) { //If remove btn pressed 
    e.preventDefault(); 
    var id = this.id; 
    var url = "process_ajax4.php"; 

    var formdata = $('.myForm').serialize(); 
    formdata += "&btn=btn_remove"; // added the btn 
    $.post(url, formdata, 
     function(data) { 

      $("#results").html(data); //Response 

     }); 
    }); 

});