Get javascript variable in 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.

Brett DeWoody

56.7k28 gold badges134 silver badges183 bronze badges

asked Mar 20, 2012 at 15:01

1

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

answered Mar 20, 2012 at 15:15

Chris CummingsChris Cummings

1,5282 gold badges24 silver badges39 bronze badges

4

Add a cookie with the javascript variable you want to access.

document.cookie="profile_viewer_uid=1";

Then acces it in php via

$profile_viewer_uid = $_COOKIE['profile_viewer_uid'];

skuntsel

11.5k11 gold badges43 silver badges67 bronze badges

answered Sep 24, 2015 at 6:07

humbleiamhumbleiam

8092 gold badges9 silver badges31 bronze badges

Here is the Working example: Get javascript variable value on the same page.

<script>
var p1 = "success";
</script>

<?php
echo "<script>document.writeln(p1);</script>";
?>

answered Jul 25, 2014 at 7:21

Get javascript variable in php

Arslan TabassumArslan Tabassum

8661 gold badge10 silver badges25 bronze badges

10

You might want to start by learning what Javascript and php are. Javascript is a client side script language running in the browser of the machine of the client connected to the webserver on which php runs. These languages can not communicate directly.

Depending on your goal you'll need to issue an AJAX get or post request to the server and return a json/xml/html/whatever response you need and inject the result back in the DOM structure of the site. I suggest Jquery, BackboneJS or any other JS framework for this. See the Jquery documentation for examples.

If you have to pass php data to JS on the same site you can echo the data as JS and turn your php data using json_encode() into JS.

<script type="text/javascript>
    var foo = <?php echo json_encode($somePhpVar); ?>
</script>

answered Mar 20, 2012 at 15:07

floriankfloriank

25.4k9 gold badges41 silver badges66 bronze badges

1

If you want to use a js variable in a php script you MUST pass it within a HTTP request.

There are basically two ways:

  • Submitting or reloading the page (as per Chris answer).
  • Using AJAX, which is made exactly for communicating between a web page (js) and the server(php) without reloading/changing the page.

A basic example can be:

var profile_viewer_uid = 1;
$.ajax({
  url: "serverScript.php",
  method: "POST",
  data: { "profile_viewer_uid": profile_viewer_uid }
})

And in the serverScript.php file, you can do:

 $profile_viewer_uid = $_POST['profile_viewer_uid'];
 echo($profile_viewer_uid);
 // prints 1

Note: in this example I used jQuery AJAX, which is quicker to implement. You can do it in pure js as well.

answered Oct 19, 2017 at 12:16

Get javascript variable in php

T30T30

10.6k6 gold badges50 silver badges57 bronze badges

1

PHP runs on the server. It outputs some text. Then it stops running.

The text is sent to the client (a browser). The browser then interprets the text as HTML and JavaScript.

If you want to get data from JavaScript to PHP then you need to make a new HTTP request and run a new (or the same) PHP script.

You can make an HTTP request from JavaScript by using a form or Ajax.

answered Mar 20, 2012 at 15:04

QuentinQuentin

872k121 gold badges1169 silver badges1282 bronze badges

These are two different languages, that run at different time - you cannot interact with them like that.

PHP is executed on the server while the page loads. Once loaded, the JavaScript will execute on the clients machine in the browser.

answered Mar 20, 2012 at 15:05

BenOfTheNorthBenOfTheNorth

2,8841 gold badge19 silver badges46 bronze badges

In your html form make a hidden field

<input type="hidden" id="scanCode" name="SCANCODE"></input>

Then in your javascript update the field value by adding;

document.getElementById("scanCode").setAttribute('value', scanCode);

answered Mar 26, 2021 at 7:40

Get javascript variable in php

This could be a little tricky thing but the secure way is to set a javascript cookie, then picking it up by php cookie variable.Then Assign this php variable to an php session that will hold the data more securely than cookie.Then delete the cookie using javascript and redirect the page to itself. Given that you have added an php command to catch the variable, you will get it.

answered Jul 15, 2015 at 15:57

Get javascript variable in php

You need to add this value to the form data that is submitted to the server. You can use

<input type="hidden" value="1" name="profile_viewer_uid" id="profile_viewer_uid">

inside your form tag.

answered Mar 20, 2012 at 15:26

ShrieksShrieks

1411 silver badge6 bronze badges

1

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

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 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 store JavaScript value in PHP?

Solution 1.
Add a hidden field. HTML. <input type="hidden" id="btnClickedValue" name="btnClickedValue" value="" />.
Store the button inner text into a hidden field each time the button clicked. JavaScript. ... .
Then, on the backend / php side, you can write something like below to get the button innerText..

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.