简体   繁体   中英

Using jQuery's .get() to retrieve PHP data

I'm using jQuery's .ajax() to post to a PHP file called process.php. Process.php has a lot of code in it, but for simplicity's sake, let's just say it contains <?php echo 'hello'; ?> <?php echo 'hello'; ?> .

Is this the proper jQuery to insert process.php's results into div.results ? :

$.get('process.php', function(data) {
    $('.results').html(data);
});

So far it doesn't seem to be working.

Here's the HTML/Javascript file

<!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="UTF-8" />
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("form#form").submit(function() {
                var username = $('#username').attr('value');
                $.ajax({
                    type: 'POST',
                    url: 'process.php',
                    data: 'username=' + username,
                    success: function() {
                        $('form#form').hide(function() {
                            $.get('process.php', function(data) {
                                $('.results').html(data);
                            });
                        });
                    }
                });
                return false;
            });
        });
    </script>

</head>

<body id="body">

<form id="form" method="post">
    <p>Your username: <input type="text" value="" name="username" id="username" /></p>
    <input type="submit" id="submit" value="Submit" />
</form>

<div class="results"></div>

</body>

</html>

Here's process.php (greatly simplified):

<?php
    /* get info from ajax post */
    $username = htmlspecialchars(trim($_POST['username']));
    echo $username;
?>

If you simply want to place the resulting string back into an element, use load() .

$('.results').load('process.php');

However, looking at your code...

$.ajax({
    type: 'POST',
    url: 'process.php',
    data: 'username=' + username,
    success: function() {
        $('form#form').hide(function() {
            $.get('process.php', function(data) {
                $('.results').html(data);
            });
        });
    }
});

...shows you have misunderstood something. The correct anonymous function to assign to the success callback would be...

function(data) {
   $('form#form').hide()
   $('.results').html(data);
}

You could try something like this.

function ajax_login() {
    if ($("#username").val()) {
    $.post("/process.php", { username : $("#username").val() }, function(data) {
    if (data.length) {
        $("#login_form").hide();
        $("#login_result").html(data);
        }
    })
    } else {
        $("#login_result").hide();
    }

Then in process.php just echo out some text if the post sucesses.

process.php =>

if (isset($_POST['username'])
{
    echo 'hello '.$_POST['username'];
}

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