如何将带有AJAX的foreach循环中的值传递给另一个页面? [英] How do I pass value from foreach loop with AJAX to another page?
本文介绍了如何将带有AJAX的foreach循环中的值传递给另一个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的帖子系统显示的是 foreach循环
。我尝试为按钮实现 AJAX
以避免页面刷新。
我真正的问题是当我执行AJAX POST从PHP foreach循环传递一个值
然后它会只传递PHP foreach循环中的一个值
(最后一个值)。
我想传递一个值从foreach循环到第2页。
我尝试过:
来自 foreach
循环的表单
:
< form class =upVoteaction =../ inc / handlers / up-vote.phpmethod =post>
< input type =hiddenname =plusvalue ='。$ postIdentifier [$ commentID]。'/>
< button id =plustype =submitvalue ='。$ postIdentifier [$ commentID]。'>< / button>< / form>
AJAX
代码:
$(document).ready(function(){
$(。upVote)。submit(function(){
var plus = $(这个).val();
$ .ajax({
url:../inc / handler / up-vote.php,
data:$(。upVote) .serialize(),
类型:POST,
dataType:'json',
成功:函数(e){
console.log(JSON.stringify(e) );
},
错误:函数(e){
console.log(JSON.stringify(e));
console.log('error');
}
});
返回false;
});
});
另一页:
$ _ POST ['plus'];
解决方案
postIdentifier
ç ommentID]。'/>
< button id =plustype =submitvalue ='。
postIdentifier
I have a post system that displaying with a foreach loop
. I try to implement AJAX
for a button to avoid page refresh.
My real problem is when I do AJAX POST to pass a value from the PHP foreach loop
then it will only pass one value from the PHP foreach loop
(the last value).
I want to pass a value from a foreach loop to the page 2.
What I have tried:
The form
from the foreach
loop:
<form class="upVote" action="../inc/handlers/up-vote.php" method="post"> <input type="hidden" name="plus" value="'.$postIdentifier[$commentID].'" /> <button id="plus" type="submit" value="'.$postIdentifier[$commentID].'" ></button></form>
The AJAX
code:
$(document).ready(function(){ $(".upVote").submit(function(){ var plus = $(this).val(); $.ajax({ url: "../inc/handlers/up-vote.php", data: $(".upVote").serialize(), type: "POST", dataType: 'json', success: function (e) { console.log(JSON.stringify(e)); }, error:function(e){ console.log(JSON.stringify(e)); console.log('error'); } }); return false; }); });
The other page:
$_POST['plus'];
解决方案
postIdentifier
commentID].'" /> <button id="plus" type="submit" value="'.
postIdentifier
这篇关于如何将带有AJAX的foreach循环中的值传递给另一个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文