All TalkersCode Topics

Follow TalkersCode On Social Media

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

Passing JavaScript Variable To PHP In The Same Page

Last Updated : Mar 11, 2024

Passing JavaScript Variable To PHP In The Same Page

In this tutorial we will show you the solution of passing JavaScript variable to PHP in the same page, here we need to pass javascript variable to php in the same page so we need to embedded script code on php block by script tag.

As we know in javascript using document object service with writeln() method we can print anything on webpage so we just embedded that line on php by defined them between script tag.

Step By Step Guide On Passing JavaScript Variable To PHP In The Same Page :-

Here we defined html block with script within this we defined variable ‘str’ with ‘var’ keyword.

It holds string ‘Hi Everyone’ then we know for printing in script we need to use ‘document.writeln()’ method with the help of document service.

Then in php block we need to embedded this line with passed variable ‘str’ as parameter in this function so we can successfully displayed on javascript variable string on webpage.

<html>
    <head>
        <title>Passed variable javascript to php</title>
</head>
    <body>
        <script>
            var str="Hi Everyone";
        </script>
    </body>
</html>
<?php
echo "<script>document.writeln(str);</script>";
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. In html body block we defined script tag and within that we created variable ‘str’ using ‘var’ keyword then we stored string ‘Hi Everyone’ on variable ‘str’.
  4. We can also declare variable name as our wish with any other strings. When we need to pass javascript variable on php at same page we need to embedded script code on php block then only we can achieve result.
  5. For embed script code we need to define ‘document.writeln()’ line between script tag within double quotes. In this method we need to pass variable ‘str’ so we can easily display them on webpage successfully.
  6. In php code we need to use echo() method for printing them on webpage. Here we passed string value you can try to explore some other types of values also we gets successful result on webpage.
  7. So now we can exchange variable values between javascript and php. Using this we can also interact with server and webpage for do updates or something like that.

Conclusion :-

In conclusion we are able to know how to pass javascript variable on php in same page.

First we need to start our xampp server then we load this program on browser we can see result of passed variable str message of ‘Hi Everyone’ on webpage.

We can pass any string between javascript and php at same page by using this same concept result won’t get affected we can get successful result on webpage.

By using this concept we can also validate them and using javascript methods we can do more different types of process.

I hope this tutorial on passing JavaScript variable to PHP in the same page helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪