All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Use JavaScript Variable In PHP

Last Updated : Mar 11, 2024

How To Use JavaScript Variable In PHP

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>
  1. First, we write <! DOCTYPE html> to mention the version of HTML file
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Attach an external CSS file using <link> tag for styling the form.
  5. Thirdly, the <body> tag is used to define the webpage body.
  6. <h1> tag used to add heading here and also adding the inline CSS here.
  7. Opening <script> tag using JavaScript within it.
  8. Creating two empty array nam1 and nam2 using var
  9. Adding id, name, city to both of the arrays
  10. Create another empty array named names. Push both of the arrays into it
  11. Using console.log() to display it
  12. $.ajax to add URL of php file, set the method post.
  13. Set the data with JSON.Stringify() of names array
  14. 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() ;
}
?>
  1. Write <?php to write php function and close tag with ?>
  2. $names with json_decode() using super global variable $_POST
  3. Creating if else statement with if statement of json_last_error() function. Print_r() to display the name of the Name array
  4. 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.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪