How to assign value to a PHP variable using JavaScript?

0

Well, I have the following problem:

I have a table with several records, and once a certain link is clicked on the record, a modal window with more information about it is opened.

Link:

<a class="navbar-brand" href="#my_modal" data-toggle="modal" data-book-id="' . $res->codigoLO . '"><i class="fa fa-times"></i></a>

The following script takes a value that is sent per parameter when you click the link and the arrow in an input in the modal window. In fact, I would like that instead of this value be placed in input, be assigned directly to a PHP variable also present in the modal window.

     <script type="text/javascript">
        $('#my_modal').on('show.bs.modal', function (e) {
            var bookId = $(e.relatedTarget).data('book-id');
            $(e.currentTarget).find('input[name="bookId"]').val(bookId);
              });
    </script>

Any ideas how I can do this?

    
asked by anonymous 12.03.2015 / 19:05

1 answer

0

Search for Ajax (Asynchronous Javascript and XML). There are several ways we can use it, and one of those ways allows you to pass values to another file (PHP, in your case) and call it into the current page without needing to reload the page and is used a lot when we need to manipulate modal. Here is an example of a code taken from the jQuery documentation regarding using Ajax however using the jQuery library:

$.ajax({
method: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }})
.done(function( msg ) { 
alert( "Data Saved: " + msg ); 
});

And the return of the other information (attributes) from the registry to the client side could be done using JavaScript Object Notation (JSON), a code I use to convert an object, array, or variable to JSON in PHP is: p>

echo json_encode($lista_json)

And that would return one or several elements for JavaScript to handle on the page as per our requirements.

Some interesting links on these subjects:

jQuery: link

W3Schools AJAX: link

W3Schools JSON: link

    
12.03.2015 / 19:32