从PHP中获取价值显示在请求AJAX数据的页面上

时间:2011-11-21 05:06:41

标签: php ajax jquery

我有一个简单的登录表单,它确实返回了函数中的“成功”文本,但现在我希望能够添加.php文件中提供的文本。我怎么能这样做?

HTML

 <script>
  $(function(){
      $("#submitlogin").click(function() {


        inputs = {
            "logInUsername" : $('input[name=logInUsername]').val(),
            "logInPassword" : $('input[name=logInPassword]').val()
        };
        // since this is a username and password combo you will probably want to use $.post
        $.ajax ({
            type: "POST",           
            url: "loggnow.php",
            data: inputs,
            success: function() {
                $("#login").html("You are now logged in!");
            },
            error : function(jqXHR, textStatus, errorThrown){
                alert("error " + textStatus + ": " + errorThrown);
            }
        });
      });
      });

2 个答案:

答案 0 :(得分:2)

    $.ajax({
        type: "POST",           
        url: "loggnow.php",
        data: inputs,
        dataType: "html",
        success: function (data) {
            $("#login").html(data);
        },
        error : function (jqXHR, textStatus, errorThrown) {
            alert("error " + textStatus + ": " + errorThrown);
        }
    });

答案 1 :(得分:0)

jquery网站上的文档说,sucess函数将传递数据。 (见报价)。因此,您只需使用“data”变量中传回的内容,并将其放在那里。

success(data, textStatus, jqXHR)

A function to be called if the request succeeds. The function gets passed 
three arguments: The data returned from the server, formatted according to 
the dataType parameter; a string describing the status; and the jqXHR 
(in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery 1.5, the success 
setting can accept an array of functions. Each function will be called in 
turn.