如何通过jQuery.ajax()将多个参数传递给PHP?

use*_*190 2 html php jquery

我有一个简单的加载更多样式脚本,在索引页面上工作正常,其中只有一个参数通过ajax发送

    $(function() {//When the Dom is ready
    $('.load_more').live("click",function() {//If user clicks on hyperlink with class  name = load_more
        var last_msg_id = $(this).attr("id");//Get the id of this hyperlink this id indicate the row id in the database
        if(last_msg_id!='end'){//if  the hyperlink id is not equal to "end"
            $.ajax({//Make the Ajax Request
                type: "POST",
                url: "index_more.php",
                data: "lastmsg="+ last_msg_id,
                beforeSend:  function() {
                    $('a.load_more').html('<img src="loading.gif" />');//Loading image during the Ajax Request
                },
                success: function(html){//html = the server response html code
                    $("#more").remove();//Remove the div with id=more
                    $("ul#updates").append(html);//Append the html returned by the server .
                }
            });
        }
        return false;
    });
});
Run Code Online (Sandbox Code Playgroud)

有了这个HTML/PHP

<div id="more">
 <a  id="<?php echo $msg_id; ?>" class="load_more" href="#">more</a>
</div>
Run Code Online (Sandbox Code Playgroud)

不过,我想补充另一个PHP变量,因此,它也可以与特定类别的工作,我没有问题编写HTML和PHP,但我是新来的jQuery和奋力编辑脚本,如果它被设置为包括附加参数.这是我正在考虑使用的HTML,只是在编辑JQuery时苦苦挣扎

<div id="more"class="<?php echo $cat_id;?>">
<a id="<?php echo $msg_id;?>" class="load_more2" href="#">more</a>
</div>
Run Code Online (Sandbox Code Playgroud)

一如往常,任何帮助都非常感谢!

sbe*_*rry 7

你可以设置

data = {onevar:'oneval', twovar:'twoval'}
Run Code Online (Sandbox Code Playgroud)

并且将发送两个键/值对.

请参阅Jquery ajax docs

如果您查看该data部分,您可以看到您可以传递查询字符串,如您,数组或对象.如果您使用的是您已经使用的相同方法,那么您的data价值就像"lastmsg="+ last_msg_id + "&otherthing=" + otherthing,


小智 5

您可以在dataajax调用的部分中传递多个URL参数.

data: "lastmsg="+ last_msg_id +"&otherparam="+ other_param
Run Code Online (Sandbox Code Playgroud)

在PHP方面,您只需按原样处理这些内容.