Reputation: 133
Have been trying to make a guestbook with jquery, ajax and php, I have been able to reed and print out everything inside the database but for some reason I cant save what I write in the database and then print it out as a post in the guestbook, if someone could see what I am doing wrong I would appreciate it! (for now I only try to get the username inside the database)
this is the 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!');
}
});
});
this is the server.php file:
$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";
}
}
and tis is the html form:
<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>
Upvotes: 0
Views: 1250
Reputation: 1469
Try replacing $GET_
and POST_
with $_GET
and $_POST
Also your field names are empty, add name="userName"
and name="message"
to your textarea HTML tags
Upvotes: 1