| Killswitch |
01-11-2010 05:50 PM |
Jquery ajax forms in PHP MVC app
I was wondering about this since I am very new to javascript and Jquery in general.
My app uses the MVC approach. Using the ajax. method for submitting a form in Jquery means I need to make the request to a php page. I have tried sending it to site.com/mycontroller/mymethod but it always fails.
If I send it over to a seperate PHP page outside of the MVC app however, it works. Strangely though, even if that page does nothing but return false it still says it works.
Maybe I am not doing this right, but here is the jquery...
Code:
$(document).ready(function(){
// Hide status message by default
$(".error").hide();
$("#error").hide();
$("#success").hide();
// Create content
$("#submit").click(function(){
// Hide status message by default
//$("#error").hide();
//$("#success").hide();
var hasError = false;
// Check that we have a title
var title = $("#title").val();
if(title == '') {
$("#title_error").show();
hasError = true;
}
// Check that we have a url
var url = $("#url").val();
if(url == '') {
$("#url_error").show();
hasError = true;
}
// Check that the full story and short story was not left blank
var short = $('#short').val();
var full = $('#full').val();
if(short == '' && full == '') {
$("#short_error").show();
$("#full_error").show();
hasError = true;
}
// Get form data
var formData = $('#create_form').serialize();
// Check that we dont have errors
if(hasError == false) {
$(this).hide();
$.ajax({
type: "POST",
url: "http://localhost/framework2/public/js/ajax/php/content_create.php",
data: formData,
cache: false,
success: function(){
$('#success').fadeIn("slow");
},
error: function() {
$('#error').fadeIn("slow");
}
});
}
else {
$('#error').fadeIn("slow");
$(this).show();
}
return false;
});
});
Anyone here help with this? I followed a mixture of tutorials to learn how to do this, but honestly most of this was thrown together without the tutorial (all the error checking stuff mostly).
Am I missing anything here? Shouldn't returning false trigger the error option in Jquery? Also, is it possible to send the request to a method in my MVC app? I would much prefer to keep this in the APP so I don't have to get a new instance of the database and have access to all my helpers.
|