Maki Maki - 1 month ago 5
jQuery Question

send jquery variable via ajax to php script

I want to call a php script (for db use) in a javascript (or jquery) to create an output that is shown in a lightbox (a div created when necessary). The php script should use a variable (a name) to perform the db requests.
So far, calling the script works fine as well as showing the created output in the lightbox.
What doesn't work is, to send the variable to my php script to work with it.

I used following js code for calling the php script and get the result back:

var name = "Igor";
$('#lbcontent').html(
$.ajax({
type: 'POST',
async: false,
url: 'info.php',
data: {
name: name
}
}).responseText
);


This code is embedded, so that the result is posted in the respective div.

My php script looks as follows:

<?php
$name = $_GET["name"];
echo test;
echo $name;
?>


So far it should just print "test" and the name from the request. But only "test" is printed and the name is ignored (or just not shown).

I suspect a problem with the 'data: {name : name}' or with the GET in php but I couldn't figure out what is wrong with it, even though I read through lots of questions on the internet.

I would appreciate your help :)
Thanks!

Answer

You're sending the data via POST but are trying to retrieve it using $_GET.

Try instead:

echo $_POST['name']