我正在尝试整理一个脚本,允许我使用ajax使用阻止的ip地址更新我的数据库,这样我就不需要离开页面了。当我按下块按钮时,它提供了成功消息,但是当我查看数据库时,日期会更新,这是使用现在完成的,所以它实际上并没有获得变量。然而其他两个字段都是空的,但我似乎无法找到问题所在,我是ajax的新手,所以任何指针都会受到赞赏,谢谢。
表格
<div id="insert_response"></div>
<!-- Form: the action="javascript:insert()"calls the javascript function "insert" into ajax_framework.js -->
<form action="javascript:insert()" method="post" class="block">
<div align="center">
<input name="id" type="hidden" id="id" value="<?php echo "$id"; ?>"/>
<input name="ip" type="hidden" id="ip" value="<?php echo "$ip"; ?>"/>
<input type="submit" name="Submit" class="block" value="Block"/> </div>
</form>
Ajax
$(document).ready(function(){
$('form.block').submit(function () {
var id = $(this).find('.id').attr('value');
var ip = $(this).find('.ip').attr('value');
// ...
});
$.ajax({
type: "POST",
url: "ajax.php",
data: "id="+ id +"& ip="+ ip,
success: function(){
$('form#block').hide(function() {$('div.success').fadeIn();});
}
});
return false;
});
Ajax框架工作
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}
var http = createObject();
var nocache = 0;
function insert() {
// Optional: Show a waiting message in the layer with ID login_response
document.getElementById('insert_response').innerHTML = "Just a second..."
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var ip = encodeURI(document.getElementById('ip').value);
var id = encodeURI(document.getElementById('id').value);
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', 'ajax.php?id='+id+'&ip='+ip+'&nocache = '+nocache);
http.onreadystatechange = insertReply;
http.send(null);
}
function insertReply() {
if(http.readyState == 4){
var response = http.responseText;
// else if login is ok show a message: "Site added+ site URL".
document.getElementById('insert_response').innerHTML = 'Added:'+response;
}
}
最后是Php
$id = htmlspecialchars(trim($_POST['id']));
$ip = htmlspecialchars(trim($_POST['ip']));
$addClient = "INSERT INTO ipdata (ip_address, ip_check_id, date_blocked) VALUES ('$ip','$id', NOW())";
mysql_query($addClient) or die(mysql_error());
答案 0 :(得分:0)
您是否曾尝试在价值为id
和ip
的javascript中进行提醒?我很确定你说的时候:
$(this).find('.id').attr('value');
您告诉它找到名为id
的类。您的实际元素没有说class="id"
而是说id="id"
使用#
代替.
请改为尝试:
var id = $('#id').attr('value');
var ip = $('#ip').attr('value');
答案 1 :(得分:0)
您回来的成功消息只是告诉您ajax调用是成功的。这意味着它连接到服务器并得到响应。它与服务器端php脚本无关。您应该检查从服务器返回的响应并查看它的内容。您可以使用firebug或只是alert()或console.log()响应来查看。
您还应该检查发送到服务器的数据。您可能正在发送空ID或IP地址。您也可以在firebug中查看此信息。
答案 2 :(得分:0)
为了将来参考,我建议只使用$('form')。serialize()来获取数据。
$.ajax({
type: "POST",
url: "ajax.php",
data: $('form').serialize(),
success: function(){
$('form#block').hide(function() { $('div.success').fadeIn();});
}
});
这样可以避免在将表单数据提取到处理脚本时出错。