检索Ajax调用多个值 [英] retrieve multiple values from ajax call
问题描述
所以,我要送经jquery.ajax数据PHP是返回一个值(即整个PHP页面,该页面只发送一个值)
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)
我想从PHP页面中弹出两个值,但我怎么能与jQuery分裂两个值吗?
I'd like to pull two values from that php page but how can I split the two values up with jquery?
我想要实现的是,
- 在按钮,点击发送列表项的信息和IDS
- 在jQuery使用AJAX发送到PHP的每个项目
- PHP的取值,并将其保存到数据库中,然后吐出了一个新的ID
- 在jQuery的保存新的ID列表项,这是previously附着。
这是我遇到,我不能让jQuery来针对老列表项的问题。由于我使用一个for循环,其结果是在函数中我永远只能针对最后一个列表项目。
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.
这里的code有问题,我认为会的工作,但事实并非如此。
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 );
});
}
由于某些原因,CURVAL变量总是最后一个到数组中。因此,要解决这个问题,我可以通过PHP发送正确的行,但我不知道如何把它们分开。所以我希望它看起来像这样的基本。
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值超出你的PHP的,还有一些方法,我觉得最流行的,虽然是使用JSON。
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:
$arr = array(
"value_1"=>46534,
"value_2"=>654
);
echo json_encode($arr);
您将得到JSON对象:
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;
})
这篇关于检索Ajax调用多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!