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

如何通过jQuery.ajax()将多个参数传递给PHP?,php,jquery,html,Php,Jquery,Html,我有一个简单的LoadMore样式脚本,它在索引页面上运行良好,其中只有一个参数通过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

我有一个简单的LoadMore样式脚本,它在索引页面上运行良好,其中只有一个参数通过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;
    });
});
$(函数(){//当Dom准备就绪时
$('.load_more').live(“单击”,函数(){//如果用户单击类名为load_more的超链接
var last_msg_id=$(this).attr(“id”);//获取此超链接的id此id指示数据库中的行id
if(last_msg_id!=“end”){//如果超链接id不等于“end”
$.ajax({//发出ajax请求
类型:“POST”,
url:“index_more.php”,
数据:“lastmsg=“+last\u msg\u id,
beforeSend:function(){
$('a.load_more').html(“”);//在Ajax请求期间加载图像
},
成功:函数(html){//html=服务器响应html代码
$(“#more”).remove();//删除id=more的div
$(“ul#updates”).append(html);//追加服务器返回的html。
}
});
}
返回false;
});
});
使用此HTML/PHP

<div id="more">
 <a  id="<?php echo $msg_id; ?>" class="load_more" href="#">more</a>
</div>

但是,我想添加另一个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>
您可以设置

data = {onevar:'oneval', twovar:'twoval'}
两个键/值对都将被发送

如果查看
数据
部分,可以看到可以像传递查询字符串、数组或对象一样传递查询字符串。如果您使用的方法与您已经使用的方法相同,那么您的
数据
值将类似于
“lastmsg=“+lastmsg\u id+”&otherthing=“+otherthing

您可以使用以下代码:

$.ajax({//Make the Ajax Request
                type: "POST",
                url: "index_more.php",
                data: {var1: "value1", var2: "value2"},
                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 .
                }
            });
$.ajax({//发出ajax请求
类型:“POST”,
url:“index_more.php”,
数据:{var1:“value1”,var2:“value2”},
beforeSend:function(){
$('a.load_more').html(“”);//在Ajax请求期间加载图像
},
成功:函数(html){//html=服务器响应html代码
$(“#more”).remove();//删除id=more的div
$(“ul#updates”).append(html);//追加服务器返回的html。
}
});

您可以在ajax调用的
数据部分传递多个URL参数

data: "lastmsg="+ last_msg_id +"&otherparam="+ other_param
在PHP方面,您只需按现有方式处理这些问题。

试试:

data: JSON.stringify({ lastmsg: last_msg_id, secondparam: second_param_value});

您可以添加更多用逗号(,)分隔的参数。

您还想添加什么参数?@user866190在HTML中使用额外的类标记是传递第二个变量的唯一方法吗?如果需要传递三个或更多参数,您会怎么做?感谢您的回复,我想知道是否必须为与第一个参数一样设置的附加参数创建一个变量..?在我的回答中,您必须创建其他参数并设置其值。您也可以像这样在字符串中传递它:
数据:“lastmsg=“+last\u msg\u id+”&otherparam=tacos”