Home php How to pass variable from js to php

How to pass variable from js to php [duplicate]

Author

Date

Category

There is a variable in js

& lt; script & gt;
 var a = 5;
& lt; / script & gt; 

Answer 1, authority 100%

Method one:
You need to place the required JavaScript code in the page or generate it on the server side, as described in the PHP manual, and pass it to the user’s browser. In this example, we will define the width and height of the user’s screen:

& lt;? php
// check the existence of the $ width and $ height variables
if (isset ($ _ GET ['width']) AND isset ($ _ GET ['height'])) {
  // if the variables exist, then display the obtained values ​​on the screen
  echo 'Screen width:'. $ _GET ['width']. "& lt; br / & gt; \ n";
  echo 'Screen height:'. $ _GET ['height']. "& lt; br / & gt; \ n";
}
// if the variables do not exist, then do the following
else {
  // PHP will generate JavaScript code that will be processed by the browser
  // user and pass the values ​​back to the PHP script via the HTTP protocol
  echo "& lt; script language = 'javascript' & gt; \ n";
  echo "location.href = \" $ {_ SERVER ['SCRIPT_NAME']}? $ {_ SERVER ['QUERY_STRING']} "
      ... "width = \" + screen.width + \ "& amp; height = \" + screen.height; \ n ";
  echo "& lt; / script & gt; \ n";
}
? & gt;

If the user’s browser supports JavaScript, then after executing the script, the page will be refreshed and we will see the request in the address bar:

http: //path_to_script/script_name.php? width = 1024 & amp; height = 768

The ? after the script name tells the web server that we want to make a GET request to our PHP script, followed by the name of our variable and an equal sign, which assigns the next value to the variable, as defined by the JavaScript function. The & amp; serves as a separator for variables in the request. As a result, two variables will be passed to PHP: $ width with a value equal to the screen width (1024) and $ height with a value equal to the screen height (768).

And on the screen we will see the result of the script execution.

Received result: user screen width and height

Method two:
We write JavaScript code and execute it on the user side, and then transfer the result through the HTTP protocol to PHP:

& lt; script language = "javascript" & gt; & lt;! -
query = 'width =' + screen.width + '& amp; height =' + screen.height;
// - & gt; & lt; / script & gt;

Here we have assigned the query variable with the values ​​of the user’s screen width and height, as in the previous example. As a result of executing this code, the query variable will be assigned the string width = 1024 & amp; height = 768 (at a screen resolution of 1024×768).

Now we need to pass the query variable from JavaScript to PHP. We will pass the request to a PHP script named script.php. To do this, we will use an HTML tag that inserts images into an HTML page. Instead of a picture, we will indicate the name of our PHP script and attach the query variable to it with a request:

& lt; script language = "JavaScript" & gt; & lt;! -
document.write ('& lt; img src = "script.php?' + query +
'"' + 'border =" 0 "width =" 1 "height =" 1 "/ & gt;');
// - & gt; & lt; / script & gt;

In this way, you can send requests to any script. The result of executing these two blocks of JavaScript code will turn the user’s browser into the following HTML code:

& lt; img src = "script.php? width = 1024 & amp; height = 768" border = "0" width = " 1 "height =" 1 "/ & gt;

The browser will contact the server for a picture at the specified address and, as a result, will make a GET request to script.php with the parameters we need. We can now process the resulting variables in our script.php:

& lt;? php
// check for $ width and $ height variables
if (isset ($ _ GET ['width']) AND isset ($ _ GET ['height'])) {
    // Here we write the code that will be executed if the variables
    // $ width and $ height exist. They, for example, can be written in
    // text file or add to database
}
else {
    // Here we write the code that will be executed if the variables are not
    // exist or may not be used at all else {}
}
? & gt;

Method three:
The data will be transferred to the PHP script after the user clicks on the link:

& lt; script language = "javascript" & gt; & lt;! -
query = 'width =' + screen.width + '& amp; height =' + screen.height;
// - & gt; & lt; / script & gt;
& lt; script language = "javascript" & gt; & lt;! -
document.write ("& lt; a href = script.php?" + query + "& gt; link & lt; / a & gt;");
// - & gt; & lt; / script & gt;

or on a button in the form:

& lt; script language = "javascript" & gt; & lt;! -
width = screen.width;
height = screen.height;
// - & gt; & lt; / script & gt;
& lt; script language = "javascript" & gt; & lt;! -
document.write ('& lt; form name = "form1" action = "script.php" method = "get" & gt;');
document.write ('& lt; input type = "hidden" name = "width" value = "' + width + '" & gt;');
document.write ('& lt; input type = "hidden" name = "height" value = "' + height + '" & gt;');
document.write ('& lt; input type = "submit" value = "Submit" & gt; & lt; / form & gt;');
// - & gt; & lt; / script & gt;

When using a form, each variable must be passed in a separate field. We have used hidden form fields whose names match the variable names in our PHP script. Or you can not use text fields at all, but pass all the data through the action = “” parameter of our form:

& lt; script language = "javascript" & gt; & lt;! -
query = 'width =' + screen.width + '& amp; height =' + screen.height;
// - & gt; & lt; / script & gt;
& lt; script language = "javascript" & gt; & lt;! -
document.write ('& lt; form name = "form2" method = "get" action = "script.php?' +
query + '"& gt; & lt; input type =" submit "name =" submit "value =" Submit "& gt; & lt; / form & gt;');
// - & gt; & lt; / script & gt;

Conclusion

The first method is not very convenient, since the browser, after processing the JavaScript code, needs to refresh the page by passing the received data to the PHP script. When using the second method, the transfer of data from JavaScript to PHP goes unnoticed by the user without refreshing the page content. The third method gives the same result as the first two, but requires the user to click a link or button. Which is the best way to use? It all depends on the task at hand.

Any of these methods can be used to pass a value from JavaScript not only to PHP, but also to Perl, ASP, or any other programming language that runs on the web server side.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions