Pass input value to PHP variable

Asked

Viewed 6,142 times

3

I have the following problem:

I’m using Javascript to set a input guy hidden in a modal window. Now, I need to assign the value that was filled in that field to a PHP variable. I am 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 take the value of this field and assign it to the variable?

  • 2

    Welcome to Stack Overflow! You probably need to submit a request to access this variable through $_POST or $_GET

  • Something like: $x = "<script>document.write($bookId);</script>", in which $bookId has the value of $_POST['bookId'] already verified.

  • The input is not inside any form? You send the value of the field to the server via AJAX?

  • The input is not in any form and its value is not sent to the server , I only use it because I need to somehow save a value that was sent via javascript to the modal window. I wonder if there is a way to recover the value of this field and assign it to a PHP variable without accessing it by POST or GET.

1 answer

1

Well, come on!

When will you access such value in your PHP file? If you are not going to access such a value in PHP just store it in a Javascript variable (a global one).

To define a global variable just declare it outside of some function or (1), if the declaration is in a function one should not use the var (2). Ex:

1:

var valor = 10;

function foo() {
    return 'bar';
}

2:

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

If you really need to access such a value in your PHP file, just set the value input with the PHP value and add a id to identify the field (if you are going to make an Ajax request) or add the 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 value in your HTML just call functions in events, for example, onClick of some button, onKeyPress of some input, etc.

Hope I helped, hugs.

  • Fabricio, I don’t think I was clear, let me explain. I have a table on a page, and in each row of it there is an icon that when clicked opens a modal window. Within this modal window you will see other information about this record. Using Javascript I send to the modal window the primary key referring to the clicked record. I needed to assign the value of this key to a PHP variable declared within the modal window, and then perform a search to display other data. As I cannot assign this value directly to the PHP variable ...

  • I’m setting this value to an input, and then trying to take that value and assign it to the PHP variable. A crazy gambiarra! In short: I need a PHP variable within my modal to receive a value passed by Javascript.

  • Hi John Paul, is the modal open after the answer of an Ajax request? If so, take advantage of this request to send the value to PHP and add it to the modal. An option would also persist such a value in a session key ($_SESSION['your value']), whenever the modal is reopened you rewrite this value, and, to use it in PHP you just retrieve it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.