简体   繁体   中英

How can I return the data submitted from a jQuery Ajax request?

I'm really lost on how to return a value previously submitted to a PHP file with AJAX jquery. I am just starting to learn jquery so I am still struggling with concepts a bit.

Here is my code for jquery:

$('#button2').click(function(e) {
    $.post("status.php", 
        {name: $('#name').val(),
        email: $('#email').val()},
        function(data){
            $('#message').html(data);
        });
    return false;
});

$('#button3').click(function(e){
    $.get({
        url: "status.php",
        data: 'information=',
        success: function(data) {
            $('#content').data(data); },
            datatype: "text"
    });
    return false;
});

I have the user submit their name and email to the php file, that goes through correctly and displays the output message. But I then want to have another button that displays their submission, so name: xxxxx email: xxxxx inside the #content div

Here is the PHP file

<?php
extract($_GET);
extract($_POST);

$data = array();

if(isset($name) && isset($email))
{
$data[$email] = $name;
echo "User information submitted.";
}

if(isset($information))
{
foreach($data as $key => $value)
{
    $name .= $key.",";
    $email .= $value.",";
}

echo $name."|".$email."\n";
}
?>

Our teacher has supplied us with the PHP file and we are to code the jquery and ajax. I am very lost however. Any help on how I can use $information to retrieve the users submission?

Your not requesting your $name and $email variables in your PHP file. Add this to the top:

$name = $_REQUEST['name'];
$email = $_REQUEST['email'];

On a side note your $('#button3') is using $.post and you are using type:"GET" . You will need to make a choice on what you want to use.

Note that for button3 handler, you should not use $.post, use $.get instead.

Then you can remove type: ''GET'

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