I have an html table containing values that are being generated from javascript. How do I transfer those values to php variables?
4 Answers
4 Comments
pgtips
So if my table data is like this: <td> <input type="text" name="music" size="3" > </td> with name="music" causing a js value to be placed there, how do I encode this into JSON?
saturngod
you need to write parser to generate table to json with javascript. And then you can use POST it to your PHP script.
Crescent Fresh
@saturngod: you need to write parser to generate table to json with javascript. Pardon?
saturngod
in table have html tag like style , input, b , p ,etc... So, need to write table to json code. Another idea, you can select with id for value. $("table #data1").val(), $("table #data2").val() and then use POST mehtod.
What you are going to want to do is make an AJAX request to a PHP file with the data you want somewhere in the request. You can do this with POST or GET variables. If you haven't used AJAX before I would recommend you have a look at jQuery's documentation of it.
Comments
Using jQuery, you can get the value for the input named "music" as follows:
var value = $("input[name$='music']").val(); You can then use the jQuery AJAX libraries to send it somewhere, and it will then be available to the using script in $_GET or $_POST, depending on the AJAX method used, e.g.
var value = $("input[name$='music']").val(); $.post('somewhere.php', { 'music' : value }); And the PHP would be something like
<?php if (!empty($_POST['music'])) { echo 'Received music: ' . htmlentities($_POST['music'], ENT_QUOTES, 'UTF-8'); } ?>