通过AJAX / PHP传递两个变量?

时间:2014-05-12 04:57:53

标签: php sql ajax

使用更新代码编辑:我在这里遗漏了一些东西; jQuery正在工作,但变量没有传递给查询。以下是所有更新的代码:

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

5 个答案:

答案 0 :(得分:5)

而不是使用var info = 'id=' + del_id;data: info,您应该发送如下变量:

data: {id:del_id}

未来更多,如果你想传递更多变量,你可以如下所示:

data: {id:del_id,order:ORDER_ID}

希望这有帮助。

答案 1 :(得分:1)

喜欢这个

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

答案 2 :(得分:0)

将您的数据指定为对象。每个变量应以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>

答案 3 :(得分:0)

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

答案 4 :(得分:0)

做类似

的事情
    <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>