How to pass javascript value to php variable?

I have an html table containing values ​​that are generated from javascript. How to pass these values ​​to php variables?

+3
source share
4 answers

Encode the contents of the table as JSON and send it to your PHP script.

+8
source

What you need to do is make an AJAX request to the PHP file with the data you want somewhere in the request. You can do this using the POST or GET variables. If you have not used AJAX before I recommend you check out the jQuery documentation .

+2
source

jQuery, "music" :

var value = $("input[name$='music']").val();

jQuery AJAX -, script $_GET $_POST, AJAX,

var value = $("input[name$='music']").val();
$.post('somewhere.php', { 'music' : value });

PHP -

<?php
if (!empty($_POST['music'])) {
  echo 'Received music: ' . htmlentities($_POST['music'], ENT_QUOTES, 'UTF-8');
} 
?>
+1
source

I think you most likely want to calculate the values ​​in php and return them to the html table.

If this is not what you really want, then you need to make a request to php script. You can do this using the form to be submitted.

0
source

Source: https://habr.com/ru/post/1740412/


All Articles