I tried to implement an Ajax call on a project and I can not identify the error. I have a form for posting status and I would like it as soon as I post something it does not refresh on the page and rather it shows the result, then in a div.
It turns out that it is redirected to the page and goes to / update_post.php when I return to the dashboard the result is in the Status div. and appears on the bottom div. Can you publish and show the result immediately?
Follow the code:
Dashboard
<form name="updatePost" method="post" action="update_post.php">
Nome: <input type="text" class="input-xxlarge" id="status" name="data" /> </br>
<input style="margin-left:10px" type="submit" class="btn btn-primary" value="POST" onclick="updatePost"/>
</form>
<script>
$("#updatePost").click(function(){
$.ajax({
dataType:'html',
url:"update_post.php",
type:"POST",
data:({+input+'did='+did+msg='+msg}),
beforeSend: function(data){
success: function(response) {
$('#status').html(response);
},
error: function(xhr, status, error) {
alert(xhr.responseText);
}
});
return false;});
</script>
update_post.php
<?php
session_start();
require_once('connect.php');
$msg = $_POST['data'];
$sid = $_SESSION['id'];
$did = $_POST['did'];
$type = 'user';
if(!empty($msg)){
$sql = "INSERT INTO post(SID,DID,Message,'P/U') VALUES ('$sid','$did','$msg','$type')";
if($result = mysqli_query($dbc,$sql) or die('error!!'))
{
echo 'OK';
header('http://localhost/profile.php');
}
else{
echo 'Error';
}
}
?>
There is a div that shows all the statuses below the submission form:
<div id="posts" class="span9">
<br><b>Recent Posts</b><br><br>
<?php require_once('recent_posts.php');?>
</div>