简体   繁体   中英

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!

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

Try instead:

echo $_POST['name']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM