JQuery Ajax 不将变量传递给 PHP

发布于 2025-01-17 06:10:44 字数 4613 浏览 0 评论 0原文

我想传递一些有关特定聊天的变量,以便使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文