TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Javascript, AJAX, E4X (http://www.talkphp.com/javascript-ajax-e4x/)
-   -   Jquery ajax forms in PHP MVC app (http://www.talkphp.com/javascript-ajax-e4x/5217-jquery-ajax-forms-php-mvc-app.html)

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.


All times are GMT. The time now is 06:17 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0