3

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

8

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

Sign up to request clarification or add additional context in comments.

4 Comments

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?
you need to write parser to generate table to json with javascript. And then you can use POST it to your PHP script.
@saturngod: you need to write parser to generate table to json with javascript. Pardon?
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.
2

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

1

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'); } ?> 

Comments

0

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

If this isn't what you actually want then you need to make a request to the php script. You can do this using a form that gets submitted.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.