How To Use JavaScript Variable In PHP
Last Updated : Mar 11, 2024
IN - PHP JavaScript | Written & Updated By - Pragati
In this article we will show you the solution of how to use JavaScript variable in PHP, let us understand AJAX first. So, ajax stands for Asynchronous JavaScript and XML which is an open-source framework.
It is used to communicate with servers.to call the Ajax function on page load, we must first add an external jQuery file.
We are also going to use a JSON function JSON.stringify(). this function is used to convert corresponding objects to strings or stringify them.
We also used the json_decode() function to convert objects to JSON to PHP.
Step By Step Guide On How To Use JavaScript Variable In PHP :-
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> How to use JavaScript variable in php </title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"> <script src="https://ajax.googleapis.com/ajax/libs/cesiumjs/1.78/Build/Cesium/Cesium.js"></script> </head> <body> <h1 style = "color : rgb(113, 221, 113) ;"> Welcome to TalkersCode </h1> <h3> How to use JavaScript variable in php </h3> <script> var nam1 = {} ; var nam2 = {} ; nam1.id = 1 ; nam1.name = 'peter' ; nam1.city = 'london' ; nam2.id = 2 ; nam2.name = 'thomas' ; nam2.city = 'paris' ; var names = [] ; names.push(nam1) ; names.push(nam2) ; console.log(names) ; $.ajax( { url : "read.php" , method : "post" , data : {names:JSON.stringify(names)} , success : function(response) { console.log(response) ; } }) </script> </body> </html>
- First, we write <! DOCTYPE html> to mention the version of HTML file
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Attach an external CSS file using <link> tag for styling the form.
- Thirdly, the <body> tag is used to define the webpage body.
- <h1> tag used to add heading here and also adding the inline CSS here.
- Opening <script> tag using JavaScript within it.
- Creating two empty array nam1 and nam2 using var
- Adding id, name, city to both of the arrays
- Create another empty array named names. Push both of the arrays into it
- Using console.log() to display it
- $.ajax to add URL of php file, set the method post.
- Set the data with JSON.Stringify() of names array
- Success with function() with response. Using console.log() to display the response.
PHP code
<?php $names = json_decode($_POST['names'], true) ; if (json_last_error() == JSON_ERROR_NONE) { foreach($names as names) { print_r($names->name) ; } } else { echo json_last_error_msg() ; } ?>
- Write <?php to write php function and close tag with ?>
- $names with json_decode() using super global variable $_POST
- Creating if else statement with if statement of json_last_error() function. Print_r() to display the name of the Name array
- Else statement with the json_last_error_msg().
Conclusion :-
At last, here in conclusion, here we can say that with this article’s help, we know how to use JavaScript variables in php.
I hope this article on how to use JavaScript variable in PHP helps you and the steps and method mentioned above are easy to follow and implement.