I currently have this line of code:

displaystatus('CALLER IS: '+inCallingNum);

which is being used to display a message on the page saying (e.g.) CALLER IS: 01234 567890.

What I need to do now is set the value of the inCallingNum variable in a PHP variable $user_number. Is this possible?

I tried something like this, but didnt have any luck with it:

<?php $user_number ?> = inCallingNum;

Thanks for any help

Edit:

This is where inCallingNum is set:

inCallingNum = inCallingNum.slice(inCallingNum.lastIndexOf(",")+1, inCallingNum.length);

Edit 2:

I'll try to explain what I'm trying to do more clearly. What I have at the moment is 2 pages, the main page which displays all of the information and a 2nd page which queries the database and pulls out the user's profile. When the javascript variable inCallingNum changes, I need to send this to the user_data.php page and update the information to show the new person's profile.

link|improve this question

displaystatus() is a javascript function? – DanielB May 10 '11 at 9:59
1  
You cannot set server-side variable from PHP in client-side JS.JavaScript is executed only after the PHP script already renders your page. You would need to make a new request (via AJAX) to provide the server with a value computed on client side with JavaScript. – Xion May 10 '11 at 9:59
Not really enough information to understand what you are trying to do. Where does the calling number come from? Could you post a better explanation of what is going on? – meouw May 10 '11 at 10:00
displaystatus() is a javascript function, but I don't know javascript very well at all so I don't know how it works. How would I send the javascript value using AJAX? – Daniel H May 10 '11 at 10:02
1  
You can get variables from JS to PHP with XHR (directly) or a cookie (indirectly). Both can be triggered by JS. – Rudie May 10 '11 at 10:05
show 1 more comment
feedback

3 Answers

up vote 4 down vote accepted

Php is server-side code, javascript is client-side code. It is not possibile to do what you're asking, you have to set $user_number with a call to the server.

When you are working with javascript you have already had the response from the server, so you are working with the result of the server-side actions, you cannot change the source from the result.

link|improve this answer
feedback

Like the others said: JS and PHP cannot communicate directly. You'd first need to submit the JS value to PHP somehow (e.g. via GET|POST|XHR).

Or, if the inCallingNum is NOT coming from user input, but from server side (e.g. database), you could do

<?php echo 'displaystatus(\'CALLER IS: ' . $user_number. '\');'; ?>
link|improve this answer
feedback

Since JS is run after Apache processes the PHP page, this is not possible.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.