How store javascript value in php?

You have to remember that if JS and PHP live in the same document, the PHP will be executed first (at the server) and the JS will be executed second (at the browser)--and the two will NEVER interact (excepting where you output JS with PHP, which is not really an interaction between the two engines).

With that in mind, the closest you could come is to use a PHP variable in your JS:

<?php
$a = 'foo'; // $a now holds PHP string foo
?>
<script>
    var a = '<?php echo $a; ?>'; //outputting string foo in context of JS
                                 //must wrap in quotes so that it is still string foo when JS does execute
                                 //when this DOES execute in the browser, PHP will have already completed all processing and exited
</script>
<?php
//do something else with $a
//JS still hasn't executed at this point
?>

As I stated, in this scenario the PHP (ALL of it) executes FIRST at the server, causing:

  1. a PHP variable $a to be created as string 'foo'
  2. the value of $a to be outputted in context of some JavaScript (which is not currently executing)
  3. more done with PHP's $a
  4. all output, including the JS with the var assignment, is sent to the browser.

As written, this results in the following being sent to the browser for execution (I removed the JS comments for clarity):

<script>
    var a = 'foo';
</script>

Then, and only then, will the JS start executing with its own variable a set to "foo" (at which point PHP is out of the picture).

In other words, if the two live in the same document and no extra interaction with the server is performed, JS can NOT cause any effect in PHP. Furthermore, PHP is limited in its effect on JS to the simple ability to output some JS or something in context of JS.

Is tough to guess what exactly your application is doing by looking at a little bit of code. I would recommend the following just based on what posted here.

1. Add a hidden field

<input type="hidden" id="btnClickedValue" name="btnClickedValue" value="" />

2. Store the button inner text into a hidden field each time the button clicked

document.getElementById("btnClickedValue").value = buttontext;

3. Then, on the backend / php side, you can write something like below to get the button innerText

echo $_POST['btnClickedValue'];

Again, this is just based on what posted here.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    JavaScript is the client side and PHP is the server side script language. The way to pass a JavaScript variable to PHP is through a request.

    Method 1: This example uses form element and GET/POST method to pass JavaScript variables to PHP. The form of contents can be accessed through the GET and POST actions in PHP. When the form is submitted, the client sends the form data in the form of a URL such as:

    https://example.com?name=value

    This type of URL is only visible if we use the GET action, the POST action hides the information in the URL.

    Client Side:

    <!DOCTYPE html>

    <html>

    <head>

        <title>

            Passing JavaScript variables to PHP

        </title>

    </head>

    <body>

        <h2 style="color:green;">

            GeeksforGeeks

        </h2>

        <form method="get" name="form" action="destination.php">

            <input type="text" placeholder="Enter Data" name="data">

            <input type="submit" value="Submit">

        </form>

    </body>

    </html>

    How store javascript value in php?

    Server Side(PHP): On the server side PHP page, we request for the data submitted by the form and display the result.

    <?php

    $result = $_GET['data'];

    echo $result;

    ?>

    Output:

    How store javascript value in php?

    Method 2: Using Cookies to store information:
    Client Side: Use Cookie to store the information, which is then requested in the PHP page. A cookie named gfg is created in the code below and the value GeeksforGeeks is stored. While creating a cookie, an expire time should also be specified, which is 10 days for this case.

    <script>

    // Creating a cookie after the document is ready

    $(document).ready(function () {

        createCookie("gfg", "GeeksforGeeks", "10");

    });

    // Function to create the cookie

    function createCookie(name, value, days) {

        var expires;

        if (days) {

            var date = new Date();

            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));

            expires = "; expires=" + date.toGMTString();

        }

        else {

            expires = "";

        }

        document.cookie = escape(name) + "=" + 

            escape(value) + expires + "; path=/";

    }

    </script>

    Server Side(PHP): On the server side, we request for the cookie by specifying the name gfg and extract the data to display it on the screen.

    <?php

        echo $_COOKIE["gfg"];

    ?>

    Output:

    How store javascript value in php?

    JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    How can we store JavaScript variable data in PHP variable?

    After the execution of the javascript code and after assigning the relevant value to the relevant javascript variable, you can use form submission or ajax to send that javascript variable value to use by another php page (or a request to process and get the same php page).

    How use JavaScript variable in PHP SQL query?

    php $var1 = $_POST['var1']; $var2 = $_POST['var2']; $getvalue="SELECT id,name from table1 WHERE column1='$var1' and column2='$var2'"; $result=mysql_query($getvalue) or die(mysql_error()); while($row=mysql_fetch_array($result)){ extract($row); echo $name; } ?>

    How use JavaScript variable on same page in PHP?

    You can easily get the JavaScript variable value on the same page in PHP. Try the following codeL. <script> var res = "success"; </script> <? php echo "<script>document.

    How do I assign a JavaScript variable value to PHP variable in stack overflow?

    Show activity on this post. Show activity on this post. Show activity on this post. <script type="text/javascript"> var width=screen..
    First add a cookie jquery plugin..
    Then store that window width in a cookie variable..
    Access your cookie in PHP like $_COOKIE['variable name']..