0

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?

5
  • are u sure {id: data.context.id} is setting id value. Check on web browser tool to see what value is getting sent. Commented Feb 10, 2014 at 21:24
  • is data.context.id set? what is familytree.php? is there function reading post array? why are you mixing php and jquery? Commented Feb 10, 2014 at 21:25
  • @AbhikChakraborty I did what you said and no it doesn't and that's weird, because data.context.id is the id of diagram's item that is clicked. Commented Feb 10, 2014 at 21:35
  • @M.Svrcek, data.context.id is the id of diagram's item that is clicked, it is set on each click, I would like to pass the id of clicked item to php. familytree.php is where I would like to post the value. I'm mixing php and jquery because I was trying to do many things to find out why I didn't get the value, that wasn't my original code and PHP will be used in another function when I manage to find out why I can't set the value. Commented Feb 10, 2014 at 21:40
  • I've rollbacked your inclusion of a solution in the question: please find it in the revision history and post it as an answer of its own. Commented Jul 15, 2018 at 11:10

2 Answers 2

1

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) } ); 
Sign up to request clarification or add additional context in comments.

8 Comments

Yes, I'm aware I shouldn't mix them. I've been trying to do many things to understand why I can't get the value and that's what I ended up with. That isn't my original code and PHP will be used in another function when I manage to find out why I can't set the value. data.context.id is the id of each diagram's item that I'm trying to display. It is set when the item is clicked. Why it doesn't set id value then?
The problem isn't mixing them, per se; it's that you mix them incorrectly. It is impossible for the code you have used above to ever work; you are asking the JavaScript to send POST data and then use PHP within the same JavaScript to read that POST data. That will never work; by the time the JavaScript runs, the PHP code has been executed and will not change. In other words, the POST data is evaluated when the JavaScript loads, not when you do your $.post().
I realise it but it hasn't worked before I mixed them this way. I've made a few changes to my questions and put my original code in it. Is this correct, can I mix jquery and php this way?
No, you can't. data.context.id is a JavaScript variable. $_POST['id'] is a PHP variable. They aren't the same, and my comments above apply to all three versions of your code. The PHP is evaluated before the page reaches the user, so $_POST['id'] will not be set when you do your if statement.
Thank you for answering. What I can do then? What is the solution to my problem? Is there a way to do what I need to do?
|
0

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.

1 Comment

data.context.id is the id of diagram's item that is clicked, it is set on each click, I would like to pass the id of clicked item to php. I tried to check on web browser tool now and it says that id is undefined. Any idea why is that?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.