I'm working on a simple wall post and having a hard time trouble-shooting it. I'd like it to post without having to reload the page (like a facebook post). Nothing really happens when I hit submit. The page reloads, no post appears, and nothing is inserted into the database. I can't figure out why. Any suggestions?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<HTML>
<HEAD>
<script type="text/javascript" src="js/jquery.js"> </script>
</HEAD>
<BODY>
<form id="submit_wall">
<label for="message_wall">Share your message on the Wall</label>
<input type="text" id="message_wall" />
<button type="submit">Post to wall</button>
</form>
<ul id="wall">
</ul>
<script type="text/javascript">
$(document).ready(function(){
$("form#submit_wall").submit(function() {
var message_wall = $('#message_wall').attr('value');
$.ajax({
type: "POST",
url: "insert.php",
data:"message_wall="+ message_wall,
success: function(){
$("ul#wall").prepend("<li style="display: none;">"+message_wall+"</li>");
$("ul#wall li:first").fadeIn();
}
});
return false;
});
});
</script>
</body>
insert.php is just a simple db input:
<?php
if(isset($_POST['message_wall'])){
/* Connection to Database */
$link = mysql_connect($host, $un, $pw) or die("Couldn't make connection.");
@mysql_select_db($db, $link) or die("Couldn't select database");
/* Remove HTML tag to prevent query injection */
$message = strip_tags($_POST['message_wall']);
$sql = 'INSERT INTO wall ("message") VALUES( "'.$message.'")';
mysql_query($sql);
echo $message;
} else { echo '0'; }
?>
First of all, this is quite not a question. But I something i'd like to point out is that you should use
var message_wall = $('#message_wall').val();
Instead of
var message_wall = $('#message_wall').attr('value');
Other than that, I dont see anything that could be wrong. Please explain yourself a little bit better
Here's the "wall post" part. You have to add the ajax call of course
EDIT:
$.ajax({
type: "POST",
url: "insert.php",
data:{'message_wall':message_wall}, /* This is the line i've modified */
success: function(){
$("ul#wall").prepend("<li style="display: none;">"+message_wall+"</li>");
$("ul#wall li:first").fadeIn();
}
});