Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get JavaScript variable value in PHP

Tags:

javascript

php

I want the value of JavaScript variable which i could access using PHP. I am using the code below but it doesn't return value of that variable in PHP.

// set global variable in javascript     profile_viewer_uid = 1; 

// php code  $profile_viewer_uid=$_POST['profile_viewer_uid'];  

this gives me the following error :-

A PHP Error was encountered Severity: Notice Message: Undefined index: profile_viewer_uid 

Another php code i used which give empty value

$profile_viewer_uid = "<script language=javascript>document.write(profile_viewer_uid);</script> 

When I echo it shows nothing.

like image 747
rash111 Avatar asked Mar 20 '12 15:03

rash111


People also ask

Can I use JavaScript variable in PHP?

The way to pass a JavaScript variable to PHP is through a request. This type of URL is only visible if we use the GET action, the POST action hides the information in the URL. Server Side(PHP): On the server side PHP page, we request for the data submitted by the form and display the result. $result = $_GET [ 'data' ];

How store JavaScript value in PHP?

After the execution of the javascript code and after assigning the relevant value to the relevant javascript variable, you can use form submission or ajax to send that javascript variable value to use by another php page (or a request to process and get the same php page).

How use JavaScript variable in PHP SQL query?

php $var1 = $_POST['var1']; $var2 = $_POST['var2']; $getvalue="SELECT id,name from table1 WHERE column1='$var1' and column2='$var2'"; $result=mysql_query($getvalue) or die(mysql_error()); while($row=mysql_fetch_array($result)){ extract($row); echo $name; } ?>

How use JavaScript variable on same page in PHP?

You can easily get the JavaScript variable value on the same page in PHP. Try the following codeL. <script> var res = "success"; </script> <? php echo "<script>document.


1 Answers

You will need to use JS to send the URL back with a variable in it such as: http://www.site.com/index.php?uid=1

by using something like this in JS:

window.location.href=”index.php?uid=1"; 

Then in the PHP code use $_GET:

$somevar = $_GET["uid"]; //puts the uid varialbe into $somevar 
like image 110
Chris Cummings Avatar answered Sep 22 '22 06:09

Chris Cummings