简体   繁体   English

将变量传递给PHP时,Ajax发布不起作用

[英]Ajax post doesn't work when passing variable to PHP

While reading other similar questions I've learned that to send a javascript value to PHP variable I need to use AJAX. 在阅读其他类似问题时,我了解到要将JavaScript值发送到PHP变量,我需要使用AJAX。 That's what I've done so far: 到目前为止,这就是我所做的:

function onCursorChanged(e, data) {
    $.post('familytree.php', {id: data.context.id});
    <?php
    if (isset($_POST['id'])) {
        $id = $_POST['id'];
    }
    else {
        $id = $individualid;
    }
    ?>
}

The problem is that when I check if id is posted it always goes to else statement ( id is always equal to individualid ). 问题是,当我检查是否发布了id ,它总是转到else语句( id始终等于individualid )。 However, when I change my code to this: 但是,当我将代码更改为此时:

function onCursorChanged(e, data) {
        $.post('familytree.php', {id: data.context.id, 
                                      success: function (msg){ 
                                           alert('success') },
                                      error: function (err){ 
                                           alert(err.responseText)}
                                      });
        <?php
        if (isset($_POST['id'])) {
            $id = $_POST['id'];
        }
        else {
            $id = $individualid;
        }
        ?>
    }

EDIT: the code above is mixed incorrectly because of a lot of experimenting I've been doing. 编辑:上面的代码混合不正确,因为我一直在做很多实验。 The original code: 原始代码:

<script type="text/javascript">

    function onCursorChanged(e, data) {
    $.post('familytree.php', {id: data.context.id});
    }
    </script>

        <?php
        if (isset($_POST['id'])) {
            $id = $_POST['id'];
                                }
            else {
                $id = $individualid;
            }
        $this->displayLeafEditForm ($_SESSION['username'], $id, $this->getParents($id));

Thanks to all the answers I realised that id is not set that's why I can't get the value in php. 感谢所有答案,我意识到没有设置id,这就是为什么我无法在php中获取值。 But I don'y understand why because data.context.id is the id of the item clicked and set after each click. 但是我不明白为什么,因为data.context.id是单击的项目的ID,并在每次单击后设置。

I get the message that says 'success'. 我收到消息说“成功”。 Any idea why can't I get my variable posted? 知道为什么我不能发布变量吗?

The big problem here is that you mixing PHP and JavaScript incorrectly. 这里的最大问题是您错误地混合了PHP和JavaScript。 You are expecting $_POST['id'] to be set in the JavaScript before it goes to the client. 您期望在$_POST['id']进入客户端之前在JavaScript中进行设置。 But by the time the JavaScript reaches the client, the PHP processing is already complete. 但是到JavaScript到达客户端时,PHP处理已经完成。

This means that when the $.post() happens, the server has already decided whether if (isset($_POST['id'])) is true. 这意味着,当$.post()发生时,服务器已经确定if (isset($_POST['id']))是否为真。 The server sends the output (the JavaScript) on to the client, and then no more PHP processing will happen. 服务器将输出(JavaScript)发送到客户端,然后将不再进行PHP处理。

Also, you are passing id , success , and error as data, which is almost certainly not what you want. 另外,您将idsuccesserror作为数据传递,这几乎肯定不是您想要的。 You want this: 你要这个:

$.post('familytree.php',
    {id: data.context.id},
    success: function (msg){ 
        alert('success')
    },
    error: function (err){ 
        alert(err.responseText)
    }
);

The AJAX success function cares whether the asynchronous call occurred without error. AJAX success函数关心异步调用是否发生而没有错误。 The fact that you even reached if (isset(...)) (and didn't set any error code eg. with header(...) ) means that the AJAX call succeeded. 您甚至到达if (isset(...)) (并且未设置任何错误代码,例如带有header(...) )的事实意味着AJAX调用成功。

If isset is returning false, you need to look closer at the information that your AJAX call is actually sending to the PHP. 如果isset返回的是false,则需要仔细查看AJAX调用实际上发送给PHP的信息。 Try putting in print_r($_POST) to see what post values you're actually getting. 尝试放入print_r($_POST)以查看您实际获得的帖子值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM