我如何发送参数到php脚本(ajax),使他们将包括在服务器端

How can I send parameters to php script (with ajax) so that they will be included on the server side?

本文关键字:他们将 包括 服务器端 ajax 何发送 参数 脚本 php      更新时间:2023-09-26

我有以下功能:

function myFunction () {
    $.getJSON('remote.php', function(json) {
        var messages = json;
        function check() {
             ...        

,我在那里调用远程.php脚本,它使一个简单的选择查询,并返回json的所有数据。我想向这个名为time的查询传递一个参数,该参数将在前面的代码中填充:

var actualTime = new Date(params);

我知道在php脚本中我必须这样做:

$time = $_POST['time'];

但我应该如何修改我的jquery然后传递这个参数进一步?

将对象传递给$.getJSON。它将作为$_GET发送到PHP。

$.getJSON('remote.php', {
    time: actualTime.toJSON()
}, function(json) {
    var messages = json;
});

那么您的日期将在PHP中作为$_GET['time']。我建议转换为DateTime对象,以便您可以根据需要对其进行格式化。

$time = new DateTime($_GET['time']);

如果你想用$_POST代替,那么你必须改为使用$.post

$.post('remote.php', {
    time: actualTime.toJSON()
}, function(json) {
    var messages = json;
}, 'json');
相关文章: