0

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!

Maki
  • 109
  • 13
  • 1
    `$name = $_POST["name"];` not `$_GET` – ArmKh Nov 01 '16 at 14:10
  • Possible duplicate of [jQuery Ajax POST example with PHP](http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) – Masivuye Cokile Nov 01 '16 at 14:12
  • The AJAX request needs to be in an anonymous function you provide to `html()`. Also, remove `async: false`. It's terrible practice to use it, and you'll see warnings in your console about its use – Rory McCrossan Nov 01 '16 at 14:14

1 Answers1

1

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

Try instead:

echo $_POST['name']