一直在試圖做一個留言簿與jQuery的,ajax和PHP,我已經能夠蘆葦和打印出數據庫內的一切,但由於某種原因,我不能保存我在數據庫中寫入,然後打印它作爲留言板上的一篇文章,如果有人可以看到我在做什麼錯誤,我將不勝感激! (現在我只能儘量讓數據庫中的用戶名)留言ajax php jquery
這是jQuery的:
$("#newPost").bind('click', function(){
var userName = $('#userName').val();
var message = $('#message').val();
$.ajax({
url: "server.php?action=newPost",
type: "POST",
data: {userName: userName},
success: function(data){
if(data == "true"){
alert(data);
$('#posts').prepend('<td>'+userName+'</td>');
$('#userName').val('');
}
else{
alert('Something went wrong while trying to save!');
}
},
error: function(xhr, error){
alert('Could not connect to server!');
}
});
});
這是server.php文件:
$db = mysqli_connect('localhost', 'username', 'password', 'my_database');
if(isset($GET_['action']) && $GET_['action'] == 'newPost'){
$userName = mysqli_real_escape_string($db, POST_['userName']);
if(mysqli_query($db, "INSERT INTO message (name) VALUES ('$userName')")){
echo "true";
}
else{
echo "false";
}
}
和TIS是HTML形式:
<form action="#">
<p>Name:</p>
<textarea type="text" class="field" id="userName" rows="1" cols="20"></textarea><br/><br/>
<p>Meddelande:</p>
<textarea type="text" class="field" id="message" rows="3" cols="20"></textarea><br/><br/>
<input value="Send" class="button" type="button" id="newPost"></input><br/>
</form>
回聲用戶名,有什麼也沒有。它也應該有一個'name' – Matheno
'url:「server.php?action = newPost」,你確定這是你要訪問的文件的正確路徑嗎? 'server.php'可能位於不同的位置。 – 2013-05-17 13:43:01
它與所有其他文件在相同的文件夾... – spovell