rash111
rash111

Reputation: 1317

How to get JavaScript variable value 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.

Upvotes: 36

Views: 366604

Answers (10)

Chris van der Gaag
Chris van der Gaag

Reputation: 47

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);

Upvotes: 1

T30
T30

Reputation: 12222

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.

Upvotes: 5

humbleiam
humbleiam

Reputation: 1009

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'];

Upvotes: 24

Jatin Patro
Jatin Patro

Reputation: 1

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.

Upvotes: 0

floriank
floriank

Reputation: 25698

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>

Upvotes: 7

Arslan Tabassum
Arslan Tabassum

Reputation: 940

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>";
?>

Upvotes: 14

Shrieks
Shrieks

Reputation: 141

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.

Upvotes: -1

Chris Cummings
Chris Cummings

Reputation: 1548

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

Upvotes: 21

BenOfTheNorth
BenOfTheNorth

Reputation: 2872

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.

Upvotes: 2

Quentin
Quentin

Reputation: 943564

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.

Upvotes: 3

Related Questions