简体   繁体   English

从 ajax 调用中检索多个值

[英]retrieve multiple values from ajax call

So I'm sending data via jquery.ajax to php which is returning a one value (ie the entire php page which only sends one value)所以我通过 jquery.ajax 发送数据到 php 返回一个值(即整个 php 页面只发送一个值)

I'd like to pull two values from that php page but how can I split the two values up with jquery?我想从 php 页面中提取两个值,但如何将这两个值与 jquery 分开?

What I'm trying to achieve is,我想要实现的是,

  1. on button click send list item information and ids在按钮上单击发送列表项信息和 ID
  2. jquery uses ajax to send to php for each list item jquery 使用 ajax 为每个列表项发送到 php
  3. php takes the values and saves them to the database then spits out a new ID php 获取值并将它们保存到数据库中,然后吐出一个新的 ID
  4. jquery saves that new ID to the list item that it was previously attached to. jquery 将该新 ID 保存到它之前附加到的列表项中。

The problem that I'm running into that I can't get jquery to target the old list item.我遇到的问题是我无法获得 jquery 来定位旧列表项。 Since I'm using a for loop and the results are inside of a function I can only ever target the last list item.由于我使用的是 for 循环并且结果在 function 中,因此我只能定位最后一个列表项。

Here's the code in question, which I thought would have worked, but it didn't.这是有问题的代码,我认为它可以工作,但没有。

var i;
  for (i = 0; i < updatearray.length; ++i) {
    var curval    = updatearray[i],
        rowid     = "#row_"+curval,
        text1val  = $(rowid+" .text1").val(),
        text2val  = $(rowid+" .text2").val(),
        text3val  = $(rowid+" .text3").val();
        cardorder = $(".edit_main li:visible").index($(rowid)) + 1;

    $.ajax({
      type: "POST",
      url: "../../study/edit/save.php",
      data: { prowid: curval, ptext1val: text1val, ptext2val: text2val, ptext3val: text3val, pdeckid: deckid, pcardorder: cardorder }
    })
    .done(function( msg ) {
      $("#row_"+curval+' button').val(msg);

      alert("Data Saved2: " + msg );
    });  
  }

For some reason the "curval" variable will always be the last one in the array.由于某种原因,“curval”变量将始终是数组中的最后一个。 So, to counter this, I could send the correct row via php, but I don't know how to split them up.所以,为了解决这个问题,我可以通过 php 发送正确的行,但我不知道如何将它们分开。 So I'd want it to look like this basically.所以我希望它基本上看起来像这样。

 .done(function( newValue, oldValue ) {

   $("#row_"+oldValue+' button').val(newValue);
 });

If what I'm trying to do is unclear please say so and I'll try to give a more thorough explanation.如果我想做的事情不清楚,请这样说,我会尽力给出更详尽的解释。

you can get more than 2 values out of your php, there are some ways, i think the most popular though is with JSON. 您可以从您的PHP中获取两个以上的值,有一些方法,我认为最受欢迎的是JSON。

lets say in your php: 在您的php中说:

$arr = array(
 "value_1"=>46534,
 "value_2"=>654
);

echo json_encode($arr);

you will get the JSON object: 您将获得JSON对象:

{"value_1":46534,"value_2":"654"}

and than you can use it like this in your promise 然后您可以像这样在诺言中使用它

$.ajax(...).success(function(data){
    var value1 = data.value_1;
    var value2 = data.value_2;
})

I would do it differently I think. 我想我会做不同的事情。 I would use JSON as Dima suggest, and put all the variables in a json string when sending to php, and also back: 我将按照Dima的建议使用JSON,并在发送到php时将所有变量放入json字符串中,然后返回:

AJAX: var json_var = all the other variables you are sending in json format (please google json if you dont know how to use it) AJAX:var json_var =您以json格式发送的所有其他变量(如果您不知道如何使用它,请使用google json)

$.ajax({
      type: "POST",
      url: "../../study/edit/save.php",
      data: { vars: json_var }
    })

in php: 在php中:

$vars = json_decode($_POST['vars']); // is now an array that you can use

and for returning data: 并返回数据:

print json_encode($array);

and in JS: 在JS中:

.done(function( msg ) {
      var obj = JSON.parse(msg);
      // if you want use jquery than this instead
      var obj = $.parseJSON(msg);

      $("#row_"+curval+' button').val(obj.ptext2val);
    });  

i am facing same problem,我面临着同样的问题,

$.ajax(...).success(function(data){
    var value1 = data.value_1;
    var value2 = data.value_2;
    alert(value1);
})

when i use this,i got undefined error,but if i use alert(data);当我使用它时,我得到了未定义的错误,但是如果我使用警报(数据); it is showing this result它显示了这个结果

{"value_1":46534,"value_2":"654"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM