JQuery Ajax 不将变量传递给 PHP
我想传递一些有关特定聊天的变量,以便使用 JQuery 和 Ajax 从数据库中提取所有聊天消息。我有一个包含每个对话/聊天的隐藏输入的列表,如下所示:
<ul class="userlist">
<li class="thisuser">
<div class="thisuserimg"><img src="images/userpics/user1.png" alt=""/></div>
<div class="thischatdets">
<p class="thischatuser"><span>This is my username buddy</span><span>Mar 09</span></p>
<b class="thischatad"><span>Closed</span>This is my ad title buddy</b>
<p class="thischatmsg">This is the last msg in the chat buddy</p>
</div>
<input type="text" class="abtitem" value="has-itemid" />
<input type="text" class="abtbuyer" value="has-userid" />
<input type="text" class="abtseller" value="has-userid" />
</li>
<li class="thisuser">
<div class="thisuserimg"><img src="images/userpics/user1.png" alt=""/></div>
<div class="thischatdets">
<p class="thischatuser"><span>This is my username buddy</span><span>Mar 09</span></p>
<b class="thischatad"><span>Closed</span>This is my ad title buddy</b>
<p class="thischatmsg">This is the last msg in the chat buddy</p>
</div>
<input type="text" class="abtitem" value="has-itemid" />
<input type="text" class="abtbuyer" value="has-userid" />
<input type="text" class="abtseller" value="has-userid" />
</li>
<li class="thisuser">
<div class="thisuserimg"><img src="images/userpics/user1.png" alt=""/></div>
<div class="thischatdets">
<p class="thischatuser"><span>This is my username buddy</span><span>Mar 09</span></p>
<b class="thischatad"><span>Closed</span>This is my ad title buddy</b>
<p class="thischatmsg">This is the last msg in the chat buddy</p>
</div>
<input type="text" class="abtitem" value="has-itemid" />
<input type="text" class="abtbuyer" value="has-userid" />
<input type="text" class="abtseller" value="has-userid" />
</li>
<ul>
我在此页面上有几个 ajax 请求,如下所示:
$(document).ready(function(){
//Send Message
$("#newmsg").submit(function(e){
$.ajax({
type: "POST",
url: "server/server.php",
data: $("#newmsg").serialize(),
success:function(data){
console.log(data);
}
});
e.preventDefault();
});
//Autoreferesh Userlist
setInterval(function() { // this will execute the function every 1s
let url = "server/test.php";
$.get(url, function(d) { // jquery function that 'does' the GET request
$('.userlist').html(d) // when we have a returned request, populate the element above with the body of the returned request ( plain text )
});
}, 3000);
//Load chat messages
$("ul.userlist").on("click","li", function(){
//alert("Chat content loaded successfully!");
//alert($(this).find(".thischatuser span").text());
var abtitem=$(this).find(".abtitem").val();
var abtbuyer=$(this).find(".abtbuyer").val();
var abtseller=$(this).find(".abtseller").val();
event.preventDefault();
$.ajax({
url:"server/test.php",
method:"POST",
data:{abtitem:abtitem, abtbuyer:abtbuyer, abtseller:abtseller},
cache:false,
success:function(data){
let url = "server/test.php";
$.get(url, function(data) { // jquery function that 'does' the GET request
$('.chatarea').html(data) // when we have a returned request, populate the element above with the body of the returned request ( plain text )
});
}
});
});
});
现在,“加载聊天消息”代码负责发布和显示我需要的数据,并且它似乎正在工作,因为我正在收到来自服务器的响应。但是,服务器响应显示后变量未发送。 注意:已经尝试了几乎所有可用的答案,但没有一个有效!
I want to pass some variables about a specific chat in order to pull all the chat messages from the database using JQuery and Ajax. I have a list with hidden inputs for each conversation/chat like so:
<ul class="userlist">
<li class="thisuser">
<div class="thisuserimg"><img src="images/userpics/user1.png" alt=""/></div>
<div class="thischatdets">
<p class="thischatuser"><span>This is my username buddy</span><span>Mar 09</span></p>
<b class="thischatad"><span>Closed</span>This is my ad title buddy</b>
<p class="thischatmsg">This is the last msg in the chat buddy</p>
</div>
<input type="text" class="abtitem" value="has-itemid" />
<input type="text" class="abtbuyer" value="has-userid" />
<input type="text" class="abtseller" value="has-userid" />
</li>
<li class="thisuser">
<div class="thisuserimg"><img src="images/userpics/user1.png" alt=""/></div>
<div class="thischatdets">
<p class="thischatuser"><span>This is my username buddy</span><span>Mar 09</span></p>
<b class="thischatad"><span>Closed</span>This is my ad title buddy</b>
<p class="thischatmsg">This is the last msg in the chat buddy</p>
</div>
<input type="text" class="abtitem" value="has-itemid" />
<input type="text" class="abtbuyer" value="has-userid" />
<input type="text" class="abtseller" value="has-userid" />
</li>
<li class="thisuser">
<div class="thisuserimg"><img src="images/userpics/user1.png" alt=""/></div>
<div class="thischatdets">
<p class="thischatuser"><span>This is my username buddy</span><span>Mar 09</span></p>
<b class="thischatad"><span>Closed</span>This is my ad title buddy</b>
<p class="thischatmsg">This is the last msg in the chat buddy</p>
</div>
<input type="text" class="abtitem" value="has-itemid" />
<input type="text" class="abtbuyer" value="has-userid" />
<input type="text" class="abtseller" value="has-userid" />
</li>
<ul>
I have a couple of ajax requests on this page like so:
$(document).ready(function(){
//Send Message
$("#newmsg").submit(function(e){
$.ajax({
type: "POST",
url: "server/server.php",
data: $("#newmsg").serialize(),
success:function(data){
console.log(data);
}
});
e.preventDefault();
});
//Autoreferesh Userlist
setInterval(function() { // this will execute the function every 1s
let url = "server/test.php";
$.get(url, function(d) { // jquery function that 'does' the GET request
$('.userlist').html(d) // when we have a returned request, populate the element above with the body of the returned request ( plain text )
});
}, 3000);
//Load chat messages
$("ul.userlist").on("click","li", function(){
//alert("Chat content loaded successfully!");
//alert($(this).find(".thischatuser span").text());
var abtitem=$(this).find(".abtitem").val();
var abtbuyer=$(this).find(".abtbuyer").val();
var abtseller=$(this).find(".abtseller").val();
event.preventDefault();
$.ajax({
url:"server/test.php",
method:"POST",
data:{abtitem:abtitem, abtbuyer:abtbuyer, abtseller:abtseller},
cache:false,
success:function(data){
let url = "server/test.php";
$.get(url, function(data) { // jquery function that 'does' the GET request
$('.chatarea').html(data) // when we have a returned request, populate the element above with the body of the returned request ( plain text )
});
}
});
});
});
Now, the "Load chat messages" code is responsible for the post and display of the data I need, and it seems to be working because I'm receiving a response from the server. However, the server response shows that the post variables are not sent.
Note: Have tried nearly all the available answers on SO but none of them works!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论