I am making a php mysql insert with jquery ajax. Now I want add an effection: If insert success or not, there will also load the html from ajax process page with fadein and fadeout. How to modify? thanks.
index.php
<script type="text/javascript">
$(function(){
$("#submit").click(function(){
var formvalue = $("#content").val();
var url = 'submit=1&content=' + escape(formvalue);
$.ajax({
type: "POST",
url: "2.php",
data: url,
success: function(){
$("#content").val('');
$("#comment").fadeIn(1000, function () {
$("#comment").html(html);
});
$("#comment").fadeOut(1000, function () {
$("#comment").html('');
});
}
});
return false;
});
});
</script>
<form id="form" action="2.php" method="post">
<textarea name="content" id="content" cols="30" rows="3"></textarea> <br />
<input type="submit" id="submit" name="submit" value="Add comment" />
</form>
<div id="comment"></div>
2.php
<?php
$connection = mysql_connect('localhost', 'root' , 'root') or die(mysql_error());
$selection = mysql_select_db('my_content', $connection);
if(isset($_POST['submit'])){
$content = $_POST['content'];
$ins = mysql_query("INSERT INTO msg (content) VALUES ('$content')");
echo 'insert success';
}else{
echo 'failed';
}
?>
Well, you could use the callback you get from your backend file.
Try this instead.
EDIT: As Pendo mention below, you should use JSON when returning data from
2.php.Edited version
Index.php
2.php