Maki Maki - 10 months ago 55
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";
type: 'POST',
async: false,
url: 'info.php',
data: {
name: name

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

My php script looks as follows:

$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 :)

Answer Source

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

Try instead:

echo $_POST['name']