我有一个非常简单的表格。这么简单可怕。当我运行表单时,会返回数据和我需要的消息,但是会打开另一个空白页面来显示数据。我不知道如何将查询数据从php处理程序文件返回到成功回调。我已经尝试将其附加到表单中。我通常在codeigniter中执行此操作,而不是“那么多”很难做到。
表单和ajax
<form method="post" action="/checkipf.php" id="checkip">
<label>Enter the Address</label> <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>
处理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文件中的正确echo在空白页面上返回。我只是无法得到它成功的回调。如果我运行警报,则数据显示它正由ajax发布
感谢您的时间
答案 0 :(得分:3)
<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>
答案 1 :(得分:1)
<div id="display" style="display:none;" ></div>
然后尝试使用您在代码中完成的消息显示它。