user3182654
user3182654

Reputation: 47

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. 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). 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. But I don'y understand why because data.context.id is the id of the item clicked and set after each click.

I get the message that says 'success'. Any idea why can't I get my variable posted?

Upvotes: 0

Views: 814

Answers (2)

elixenide
elixenide

Reputation: 44831

The big problem here is that you mixing PHP and JavaScript incorrectly. You are expecting $_POST['id'] to be set in the JavaScript before it goes to the client. But by the time the JavaScript reaches the client, the PHP processing is already complete.

This means that when the $.post() happens, the server has already decided whether if (isset($_POST['id'])) is true. The server sends the output (the JavaScript) on to the client, and then no more PHP processing will happen.

Also, you are passing id, success, and error as data, which is almost certainly not what you want. You want this:

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

Upvotes: 1

Brian S
Brian S

Reputation: 5056

The AJAX success function cares whether the asynchronous call occurred without error. 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 is returning false, you need to look closer at the information that your AJAX call is actually sending to the PHP. Try putting in print_r($_POST) to see what post values you're actually getting.

Upvotes: 0

Related Questions