簡體   English   中英

通過AJAX / PHP傳遞兩個變量?

[英]Passing two variables via AJAX/PHP?

用更新的代碼編輯:我在這里遺漏了一些東西; jQuery正常運行,但變量arent傳遞給查詢。 以下是所有更新的代碼:

<span class="accepted"><a href="#" class="accept" id="<?php echo $id1; ?>" data-order="<?php echo $name; ?>"><input type="button" title="accept" value="Accept" /></a></span>

AJAX腳本

<script type="text/javascript">
$(function() {
$(".accept").click(function(){
var element = $(this);
var del_id = element.attr("id");
var order_id = element.attr("data-order");

//if(confirm("Are you sure you want to delete this?"))
//{

     $.ajax({
       type: "POST",
       url: "accept.php",
       //data: info,
       data: {id:del_id,order_id:order_id},
       success: function(){}
    //
    });
      $(this).parents(".show").animate({ backgroundColor: "#003" }, "slow")
      .animate({ opacity: "hide" }, "slow");
     //}
    //return false;
    });
    });
    </script>

新的PHP文件

include('db.php');
$id = $_POST['id'] ;
$name = $_POST['order_id'] ;
$sql = "UPDATE mgap_orders SET mgap_status = 1 WHERE mgap_ska_id = :id AND mgap_ska_report_category = :name"; 
$stmt = $pdo->prepare($sql); 

$stmt->execute(array(
':id' => '$id',
':name' => '$name'
));

而不是使用var info = 'id=' + del_id; data: info,您應該發送如下變量:

data: {id:del_id}

將來的更多,如果您想傳遞更多的變量,可以像下面這樣:

data: {id:del_id,order:ORDER_ID}

希望這可以幫助。

像這樣

$.ajax({
   type: "POST",
   url: "delete.php",
   data: {id:del_id,order:order_variable},
   success: function(){
 }

將數據指定為對象。 每個變量應以comma分隔。

data: {variablename1: value1, variablename2: value2}

因此,您的代碼應如下所示:

<script type="text/javascript">
$(function() {
$(".delete").click(function(){
var element = $(this);
var del_id = element.attr("id");
//var info = 'id=' + del_id;
if(confirm("Are you sure you want to delete this?"))
{
 $.ajax({
   type: "POST",
   url: "delete.php",
   data: { id: del_id, var2: var2, var3: var3 },
   success: function(){
 }
});
  $(this).parents(".show").animate({ backgroundColor: "#003" }, "slow")
  .animate({ opacity: "hide" }, "slow");
 }
return false;
});
});
</script>
<script type="text/javascript">
$(function() {
$(".delete").click(function(){
var element = $(this);
var del_id = element.attr("data-id");
var order_id = element.attr("data-order");
var info = 'id=' + del_id;
var order = 'order' + order_id;
if(confirm("Are you sure you want to delete this?"))
{
 $.ajax({
   type: "POST",
   url: "mod_accept.php", //Changed the processor file
   data: {info, order},
   success: function(){
 }
});
  $(this).parents(".show").animate({ backgroundColor: "#003" }, "slow")
  .animate({ opacity: "hide" }, "slow");
 }
return false;
});
});
</script>

然后在您的html中:

<span class="accepted"><a href="#" class="accept" data-id="<?php echo $id1; ?>" data-order="<?php echo $someVariable ?>"><input type="button" title="accept" value="Accept" /></a></span>

做類似的事情

    <span class="accepted">
          <a href="#" class="delete" id="<?php echo $id1; ?>" 
data-order="<?php echo $order_id;?>">
<input type="button" title="accept" value="Accept" /></a>
      </span>


<script type="text/javascript">
$(function() {
    $(".delete").click(function(){
    var element = $(this);
    var del_id = element.attr("id");
    var order_id = element.attr("data-order");
    if(confirm("Are you sure you want to delete this?"))
    {
     $.ajax({
       type: "POST",
       url: "delete.php",
       data: {id:del_id,order_id:order_id},
       success: function(){
     }
    });
      $(this).parents(".show").animate({ backgroundColor: "#003" }, "slow")
      .animate({ opacity: "hide" }, "slow");
     }
    return false;
    });
});
</script>

暫無
暫無

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

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