Returning Ajax Post Data Back to the Success
I have a very simple form. So simple its scary. When I run the form, it returns the data AND the message I require, however it opens another blank page to display the data. I have no idea how to return the query data from the php handler file to the success call back. I have tried appending it to the form. I usually do this in codeigniter and its not "all that" hard to do there.
the form and ajax
<form method="post" action="/checkipf.php" id="checkip">
<label>Enter the Address</label> 开发者_JAVA百科   <input type="text" size="30" maxlength="20" name="ip" id="ip" required /><br />
<input type="submit" value="Submit" name="submit" id="submit">          
<div id="display"></div>
<script type="text/javascript">
$(function() {
$("#checkip").submit(function(){
var data = $('#checkip').serialize();
$ajax({
    url: "checkipf.php",
    type: "POST",
    data: data,
    success: function(msg) {                        
      $('#display').html(msg).show(1000);   // is this correct?         
      }
    }); 
    return false; 
});
});
</script>
The handling php file
$ip = mysql_real_escape_string($ip);
    if ($ip = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)){
    $sql = "SELECT `ip`, `total` FROM `spammer` where `ip` = '$ip'";
    $result = mysql_query($sql) or die(mysql_error());
    $count = mysql_num_rows($result);
    while ($row = mysql_fetch_array($result)) {
        $ip = $row['ip'];
        $total = $row['total'];
    }
    if ($count > 0) {
        echo "$ip has appeared $total times";
        } else {
            echo "$ip has not been seen";           
        }
    }else{
        echo "IP does not validate";
    }
As previously noted, the correct echo in the php file is returned on a blank page. I just cant get it to the success callback. If I run an alert the data shows it is being posted by ajax
Thanks for your time
You missed a . in your function
<script type="text/javascript">
$(function() {
$("#checkip").submit(function(){
var data = $('#checkip').serialize();
$.ajax({
    url: "checkipf.php",
    type: "POST",
    data: data,
    success: function(msg) {                        
      $('#display').html(msg).show(1000);   // is this correct?         
      }
    }); 
    return false; 
});
});
</script>
<div id="display" style="display:none;" ></div>
Then try to show it with the message you receive as you done in your code.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论