0

So I'm sending data via jquery.ajax to php which is returning a one value (i.e. the entire php page which only sends one value)

I'd like to pull two values from that php page but how can I split the two values up with jquery?

What I'm trying to achieve is,

  1. on button click send list item information and ids
  2. jquery uses ajax to send to php for each list item
  3. php takes the values and saves them to the database then spits out a new ID
  4. jquery saves that new ID to the list item that it was previously attached to.

The problem that I'm running into that I can't get jquery to target the old list item. Since I'm using a for loop and the results are inside of a function I can only ever target the last list item.

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. So, to counter this, I could send the correct row via php, but I don't know how to split them up. 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.

2 Answers 2

1

you can get more than 2 values out of your php, there are some ways, i think the most popular though is with JSON.

lets say in your php:

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

echo json_encode($arr);

you will get the JSON object:

{"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;
})
Sign up to request clarification or add additional context in comments.

Comments

1

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:

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({
      type: "POST",
      url: "../../study/edit/save.php",
      data: { vars: json_var }
    })

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

.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);
    });  

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.