0

Possible Duplicate:
Get variable from PHP to JavaScript
Access a JavaScript variable from PHP

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.

6
  • displaystatus() is a javascript function? Commented May 10, 2011 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. Commented May 10, 2011 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? Commented May 10, 2011 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? Commented May 10, 2011 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. Commented May 10, 2011 at 10:05

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

-4

you can try this,

<?php $user_number = inCallingNum ?>

where inCallingNum is your javascript variable.

1 Comment

Nnno, that's not at all how it works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.