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?