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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7083155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:58:49  来源:igfitidea点击:

How to pass multiple parameters by jQuery.ajax() to PHP?

phpjqueryhtml

提问by user866190

I have a simple load more style script that works fine on the index page, where only one parameter is sent via ajax

我有一个简单的加载更多样式脚本,它在索引页上运行良好,其中只有一个参数通过 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;
    });
});

With this HTML/PHP

使用这个 HTML/PHP

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

However, I want to add another php variable so that it can also work with particular categories, I have no problems writing the HTML and PHP but I am new to Jquery and struggling to edit the script to include the additional parameter if it is set. This is the HTML that I am thinking of using, just struggling with editing the JQuery

但是,我想添加另一个 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>

As always any help is much appreciated!

与往常一样,非常感谢任何帮助!

回答by sberry

You can set

你可以设置

data = {onevar:'oneval', twovar:'twoval'}

And both key/value pairs will be sent.

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

See Jquery ajax docs

请参阅Jquery ajax 文档

If you look under the datasection, you can see that you can pass a query string like you are, an array, or an object. If you were to use the same method you already are using then your datavalue would be like "lastmsg="+ last_msg_id + "&otherthing=" + otherthing,

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

回答by sberry

You can pass multiple URL params in the dataportion of your ajax call.

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

data: "lastmsg="+ last_msg_id +"&otherparam="+ other_param

On the PHP side, you'd just process these as you already are.

在 PHP 方面,您只需按原样处理这些。

回答by Jignesh Manek

You can use this code:

您可以使用此代码:

$.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 .
                }
            });

回答by Brijesh Kansagara

Try It:

尝试一下:

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

You can add more parameters separating them by comma (,).

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