Php ajax发布等待消息

时间:2013-06-23 01:00:07

标签: php ajax

我有一个html表单来提交一些信息:

<script type="text/javascript" src="jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.7.2.custom.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#form form').submit(function(){
            $.get('data.php', $(this).serialize(), function(data){
                $('#content').html(data);
            });             
            return false;
        });
    });
    </script>
</head>
<body>
<div id="form">
    <form>
         IP : <input type="text" name="urname"><br/>
        Port : <input type="text" name="urbirth"><br/>
        <input type="submit" name="submit" value="Submit">
    </form>
</div>
<div id="content">
</div>

使用这个php:

<?php
error_reporting(0);
$name      = strtoupper($_REQUEST['urname']);
$birth     = strtoupper($_REQUEST['urbirth']);
$address   = "$name"; //Here you can specify the address you want to check ports
$port      = "80"; //Here you can specify the port you want to check from $address
$checkport = fsockopen($address, $port, $errnum, $errstr, 2); //The 2 is the time of ping in secs

//Here down you can put what to do when the port is closed
if (!$checkport) {
    echo "The port " . $port . " from " . $address . " seems to be closed."; //Only will echo that msg
} else {

    //And here, what you want to do when the port is open
    echo "The port " . $port . " from " . $address . " seems to be open."; //The msg echoed if port is open
}
?>

由于从服务器获得回复需要很长时间,因此我想在等待ajax响应时显示“等待响应”消息。我该怎么做?

1 个答案:

答案 0 :(得分:1)

您应该在此行之前显示:

  

$。get('data.php',$(this).serialize(),function(data){

并在此之后隐藏:

  

$( '#内容')的html(数据);

您还可以使用$ .ajaxStart和$ .ajaxStop来处理所有AJAJ请求。实施例

$(document).ajaxStart(function() {
   $( "#loading" ).show();
 });
$(document).ajaxStop(function() {
      $( "#loading" ).hide();
});

用这样的html

<div id="loading" style="display:none">waiting message<div>