私はデータベースをajaxを使用してブロックされたIPアドレスで更新できるスクリプトを作成しようとしています。ブロックボタンを押すとすぐに成功メッセージが表示されますが、データベースを見ると日付が更新されていますので、実際に変数を取得することはありません。しかし、他の2つのフィールドは空ですが、どこに問題があるのか分からないようです。私はajaxを初めて使っているので、どんなポインタでも感謝します。AJAX insertは複数のエントリを作成しますが、データは作成しません
フォーム
<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>
アヤックス
$(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());
私はちょうど気付いたことは、データベース内の空白スペースの後に追加のフィールドが現れていることです[object HTMLCollection] \t [obje –
]これらは、最初にそこにいなかったので、遅れているはずです –