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.

Community
  • 1
  • 1
Daniel H
  • 2,865
  • 8
  • 34
  • 32
  • `displaystatus()` is a javascript function? – DanielB May 10 '11 at 09: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 09: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
  • From where is `inCallingNum` coming from? User input? Database? Nevermind. meouw already asked for it. – Jürgen Thelen May 10 '11 at 10:07

3 Answers3

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.

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
0

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

JohnP
  • 49,507
  • 13
  • 108
  • 140
-4

you can try this,

<?php $user_number = inCallingNum ?>

where inCallingNum is your javascript variable.

antyrat
  • 27,479
  • 9
  • 75
  • 76