Mithilfe von jQuery .get() zum abrufen PHP Daten

Ich bin mit jQuery .ajax() post an eine PHP-Datei aufgerufen process.php. Process.php hat eine Menge code, aber der Einfachheit halber sagen wir einfach, es enthält <?php echo 'hello'; ?>.

Ist dies die richtige jQuery einfügen process.php's Ergebnisse in div.results? :

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

So weit scheint es nicht zu funktionieren.

Hier ist der HTML - /Javascript-Datei

<!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>

Hier process.php (stark vereinfacht):

<?php
    /* get info from ajax post */
    $username = htmlspecialchars(trim($_POST['username']));
    echo $username;
?>
  • $('.Ergebnisse').html(data); ist richtig, aber es hängt davon ab, was genau Sie wollen zu tun....es gibt andere Möglichkeiten auch, wie laden,append,prepend n nach...können Sie uns mehr Informationen über die div
InformationsquelleAutor altrier | 2011-04-21
Schreibe einen Kommentar