Forum Moderators: open

Message Too Old, No Replies

Jquery with PHP MVC

         

xKillswitchx

5:59 pm on Jan 11, 2010 (gmt 0)

10+ Year Member



Hey all, I am a javascript noob and trying to use jquery to get some ajax in my forms within a PHP MVC application.

I can send the request to a script outside of the MVC app just fine, but as soon as I try to send it to a page inside the MVC app it fails. The page I try to send to is :

http://www.example.com/content/create_page - Where create_page is a public method of a class.

Another problem is on the page that works, even if I simply return false I always get success from JQuery. I would really like to use this within the MVC app since I can reuse my database isntance and access all my helpers in validating data.

Is this at all possible? Here is my 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;
});
});

[edited by: Fotiman at 6:34 pm (utc) on Jan. 11, 2010]
[edit reason] Examplified URL [/edit]

whoisgregg

10:08 pm on Feb 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you describe how it fails?

Right now, your success and failure handlers from your $.ajax() calls are purely based on whether or not anything is returned from the url accessed. If you want your script to trigger the error handler, it'd have to send the right kind of error header (probably a 404 would do the trick). Otherwise, you'll need to actually parse the response inside the success function to determine what action to take.

Btw, sometimes I've found difficulty in using [localhost...] during testing of AJAX... You might find different results if you are running it on the actual domain.