Pass input value to PHP variable [duplicate]

3

I have the following problem:

I'm using JavaScript to set a input type hidden in a modal window. Now I need to assign the value that was filled in this field to a PHP variable. I'm not using any form, just the field alone. I tried the following code that does not work:

    <?php
        $x = "<script>document.write(bookId)</script>";
        echo $x;
    ?>  

How do I get the value of this field and assign it to the variable?

    
asked by anonymous 05.02.2015 / 17:53

1 answer

1

Well, come on!

When will you access this value in your PHP file? If you are not accessing that value in PHP just store it in a Javascript variable (a global).

To define a global variable, simply declare it outside some function or (1), if the statement is in a function, you should not use var (2). Ex:

1:

var valor = 10;

function foo() {
    return 'bar';
}

2:

function foo() {
    valor = 10;
    return 'bar';
}

In case you really need to access this value in your PHP file, simply set the value of the input to the value of PHP and add a id to identify the field (if you make an Ajax request) name to receive such value in your PHP file.

Ex:

<input type="hidden" id="meuValor" name="nameValor" value="<?php echo $valor; ?>" />

And, after already having such a value in your HTML just call functions in events, for example, onClick of some button, someKey onKeyPress, etc.

I hope to have helped, hugs.

    
06.02.2015 / 00:54