2011-04-19 81 views
0

我有一個非常簡單的形式。它簡單而可怕。當我運行表格時,它返回數據AND我要求的信息,但是它打開另一個空白頁面來顯示數據。我不知道如何將查詢數據從php處理程序文件返回給成功回調。我試圖將它附加到表單上。我通常在codeigniter中做這件事,而不是在那裏「很難做到」。返回Ajax發佈數據返回成功

<form method="post" action="/checkipf.php" id="checkip"> 
<label>Enter the Address</label> &nbsp;&nbsp; <input type="text" size="30" maxlength="20" name="ip" id="ip" required /><br /> 

<input type="submit" value="Submit" name="submit" id="submit">&nbsp; &nbsp; &nbsp; &nbsp; 
<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> 

的處理PHP文件

$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"; 
    } 

如前所述,在php文件的正確回波在空白頁上返回的形式和Ajax 。我只是無法獲得成功回調。如果我跑警報的數據顯示,它被張貼阿賈克斯

感謝您的時間

回答

2

你錯過了。在你的函數

<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> 
+0

我必須兩次失明,我沒有看到它,也沒有得到與螢火蟲的錯誤。哪一行?> – Brad 2011-04-20 00:11:31

+1

$ .ajax而不是$ ajax :) – Joshwaa 2011-04-20 00:13:15

+0

OMG謝謝。現在我覺得很愚蠢。而我的朋友是一個勝利者。 TY。 – Brad 2011-04-20 00:23:23

1
<div id="display" style="display:none;" ></div> 

然後嘗試與您收到,你在你的代碼完成的消息顯示它。

+0

不,我仍然有返回數據顯示的空白頁面 – Brad 2011-04-20 00:15:46