All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Store JavaScript Value In PHP Variable

Last Updated : Mar 11, 2024

How To Store JavaScript Value In PHP Variable

In this tutorial we will show you the solution of how to store JavaScript value in PHP variable, as we know JavaScript is a client side language whereas php is server side.

So, let us see how to pass values between these two with help of codes.

As, there are many ways with help of which we are able to store JavaScript values to php variable.

Step By Step Guide On How To Store JavaScript Value In PHP Variable :-

Let us see some from them with the example given below in which we store some values to php variable that we specify inside JavaScript.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>home page</title>
</head>
<body>
<?php
echo $variable = "<script>document.write(input)</script>";
?>
<script>
var input="Hello World!";
</script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Mainly, there are many ways with help of which we can store JavaScript value to a php variable and vice versa.
  6. This method is one of the suitable or say efficient method which we discuss in above example.
  7. Here, as you see that let first we discuss our script inside which we declare and define a variable named input using and insert value hello world! Inside this as you can see above.
  8. Now, there is more thing in body that is our php. Now let us discuss our php. Here, as we know that php starts with <?php and ends with ?>. This is the main or say basic structure of php. Our all php code is written inside these tags.
  9. Here inside we just use echo to print variable which we assigns to echo. Echo in php is usually used to print the values.
  10. Here we create a variable and echo it to print the values that we assign to this. At next after variable we see that in quotes we use script tag. That why because we want to print JavaScript value using php.
  11. Here we direct write document.write() to display text and used the variable in which we assigns value in previous script tag. We hope that you understand the codes properly.
  12. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to store JavaScript value to php variable.

I hope this tutorial on how to store JavaScript value in PHP variable helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪