Reputation: 1168
I have a very simple form for updating a user's status message. The form does work, but I am not getting any response (using firebug the response after submit is empty).
Here is the html/js:
<form id="StatusUpdateForm" method="post" action="http://www.url.com/process.php">
<input name="StatusUpdate" type="text" id="StatusUpdate" />
<input type="hidden" name="userid" value="<?=$_SESSION['user']['id']?>" />
<input type="submit" value="Update your status" />
</form>
<div id="results"></div>
<script>
/* attach a submit handler to the form */
$("#StatusUpdateForm").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $( this ),
StatusUpdate = $form.find( 'input[name="StatusUpdate"]' ).val(),
userid = $form.find( 'input[name="userid"]' ).val(),
url = $form.attr( 'action' );
/* Send the data using post and put the results in a div */
$.post( url , $("#StatusUpdateForm").serialize(),
function( data ) {
$('#results').html(data);
}
);
});
</script>
Here is process.php:
if($_POST['StatusUpdate']&&$_POST['userid']){
if( StatusMessage::set($_POST['userid'], $_POST['StatusUpdate']) ){
echo "<div id='content'>Comment updated!</div>";
} else {
echo "<div id='content'>Problem updating comment. Try again.</div>";
}
}
Upvotes: 2
Views: 194
Reputation: 1168
You cannot use absolute paths when calling ajax. Even if you are not calling an outside url.
change http://www.url.com/process.php
to process.php
Upvotes: 0
Reputation: 9836
Change return
to echo
. If it's not outputting anything there won't be any response to send!
if ($_POST['StatusUpdate'] && $_POST['userid']) {
if (StatusMessage::set($_POST['userid'], $_POST['StatusUpdate'])) {
echo "<div id='content'>Comment updated!</div>";
} else {
echo "<div id='content'>Problem updating comment. Try again.</div>";
}
} else {
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH'])
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
) {
echo "<div id='content'>Some other error!</div>";
}
}
To be extra sure it's executing, add an error callback function:
$.post(url , $("#StatusUpdateForm").serialize(), function(data) {
$('#results').html(data);
}).error(function(eventData) {
alert('error: ' + eventData); // try that
});
Or (preferred):
$.ajax({
type: 'POST',
url: url,
success: function(response) {
$('#results').html(response);
},
error: function(xhr, textStatus, errorThrown){
// alert(errorThrown);
alert(textStatus);
}
});
Upvotes: 1