pgtips
pgtips

Reputation: 1338

How do I transfer a javascript value to a php variable?

I have an html table containing values that are being generated from javascript. How do I transfer those values to php variables?

Upvotes: 3

Views: 643

Answers (4)

El Yobo
El Yobo

Reputation: 14946

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

Upvotes: 1

Robert Clark
Robert Clark

Reputation: 488

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.

Upvotes: 0

Sam Becker
Sam Becker

Reputation: 19636

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.

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798536

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

Upvotes: 8

Related Questions