2015-02-11 74 views
1

好吧,大家希望你也可以帮助我。我不会疯狂地熟悉jQuery和AJAX,可能我错过了一些非常简单的东西。我无法传递超过1个变量。我尝试了几种不同的东西,其中最接近我的工作是this,但即使这样也行不通。如何通过jQuery将多个变量传递给PHP

jQuery的

$("#bodymes ul li span[contenteditable=true]").blur(function(){ 
      var weight_id = $(this).attr("id") ; 
      var weightbody_mes = $(this).text() ; 
      $.post("../includes/bodymes_weight.php", { weightbodymes: weightbody_mes }) 
       .done(function(data) { 
        $(".weightsuccess").slideDown(200); 
        $(".weightsuccess").html("Weight Updated successfully"); 
        if ($('.weightsuccess').length > 0) { 
         window.setTimeout(function(){ 
          $('.weightsuccess').slideUp(200); 
         }, 4000); 
        } 
        // alert("Data Loaded: " + data); 
      }); 
     }); 

所以基本上如果我运行这个它会完全正常工作和我的PHP脚本将会处理它。请注意,当我启动并启用警报并显示加载的数据时,它会显示正确的信息(来自weightbodymes的信息),我可以更新数据库。但只要我再添变数{ weightbodymes: weightbody_mes, weightid: weight_id }它不会显示数据警告框加载(如果我试图表现出从它的工作这两个变量的信息,但它也只是一个变种提交给PHP是:

$weightid = $_POST['weightid']; 
    $weight = $_POST['weightbodymes']; 

    if ($insert_stmt = $mysqli->prepare("UPDATE body SET body_weight=? WHERE body_id='$weightid'")) { 
      $insert_stmt->bind_param('s', $weight); 
      // Execute the prepared query. 
      if (! $insert_stmt->execute()) { 
       header('Location: ../index.php?error=Registration failure: INSERT nwprogram1'); 
      } 
    } 

希望你能告诉我在哪里,我犯了一个错误,以及如何纠正它。 预先感谢您!

+0

futher性质的'data'对象添加:'{weightbodymes:weightbody_mes,weightid:weight_id,FOO: '棒'}' – 2015-02-11 15:39:14

+0

送东西就像这个'{'data':{'var_1':'value1','var_2':'value2'}}'然后通过$ _POST ['data'] ['var_1'],$ _POST ['数据'] ['var_2']' – NestedWeb 2015-02-11 15:39:57

+0

这应该有你想要的东西http://stackoverflow.com/questions/8191124/send-javascript-variable-to-php-variable – MrPickles 2015-02-11 15:40:00

回答

4

使用JS/JQUERY POST Ajax请求。下面是我用我所有的AJAX语法来电

var first = 'something'; 
var second = 'second'; 
var third = $("#some_input_on_your_page").val(); 


///////// AJAX //////// AJAX ////////// 
    $.ajax({ 
     type: 'POST', 
     url: 'page_that_receives_post_variables.php', 
     data: {first:first, second:second, third:third}, 
     success: function(response){ 
      alert('yay ajax is done.'); 
      $('#edit_box').html(response);//this is where you populate your response 
     }//close succss params 
    });//close ajax 
///////// AJAX //////// AJAX ////////// 

代码PHP页面page_that_receives_post_variables.php

<?php 
$first = $_POST['first']; 
$second = $_POST['second']; 
$third = $_POST['third']; 

echo 'now do something with the posted items.'; 
echo $first; //etc... 
?> 
+0

嗯...这看起来像一个解决方案,但由于某种原因它不工作,它仍然只传递一个变量。因此,例如,在一个响应中,我只是得到'weightbodymes',尽管我传递了两个变量,我的数据行看起来像这样:data:{weightid:weight_id,weightbodymes:weightbody_mes,third:third},'(I left third element并分配了变量,但没有尝试用PHP调用它)。任何其他建议? – vpetkovic 2015-02-11 16:00:16

+0

我使它与你的解决方案一起工作。也投了票。谢谢! @Stephen Carr – vpetkovic 2015-02-11 16:55:07

+0

问题是什么?为了进行调试,在通过ajax发布变量之前,只需提醒(your_variables),以便看到实际获得的内容。通常帮助我。 – technology101010 2015-02-11 19:12:14

相关问题