[Answer]-Ajax, Django, Jquery Model Form

1๐Ÿ‘

โœ…

I managed to solve the problem, the problem was with the newest jquery using different variables. Updated the code above with the answer.

Here is the code:

TEMPLATES:

<script type="text/javascript">
    // prepare the form when the DOM is ready
    $(document).ready(function() {
        $("#add_cat").ajaxStart(function() {
            // Remove any errors/messages and fade the form.
            $(".form_row").removeClass("errors");
            $(".form_row_errors").html('');
            $("#add_cat").fadeTo('slow', 0.33);
            $("#add_cat_btn").attr("disabled", "disabled");
            $("#message").addClass('hide');
        });

        // Submit the form with ajax.
        $("#add_cat").submit(function(){
            $.post(
                // Grab the action url from the form.
                "#add_cat.getAttribute('action')",

                // Serialize the form data to send.
                $("#add_cat").serialize(),

                // Callback function to handle the response from view.
                function(resp, testStatus) {
                    $(".form_row").removeClass("errors");

                    $("#trip_type_errors").html("");
                    $("#company_name_errors").html("");
                    $("#individual_name_errors").html("");
                    $("#phone_errors").html("");
                    $("#email_errors").html("");
                    $("#from_country_errors").html("");
                    $("#to_country_errors").html("");
                    $("#from_city_errors").html("");
                    $("#to_city_errors").html("");
                    $("#departure_date_errors").html("");
                    $("#return_date_errors").html("");
                    $("#number_of_passengers_errors").html("");

                    if (resp.error) {
                        // check for field errors
                        if (resp.trip_type_error != '') {
                            $("#trip_type_row").addClass('errors');
                            $("#trip_type_errors").html(resp.trip_type_error);
                        }
                        if (resp.company_name_error != '') {
                            $("#company_name_row").addClass('errors');
                            $("#company_name_errors").html(resp.company_name_error);
                        }
                        if (resp.individual_name_error != '') {
                            $("#individual_name_row").addClass('errors');
                            $("#individual_name_errors").html(resp.individual_name_error);
                        }
                        if (resp.phone_error != '') {
                            $("#phone_row").addClass('errors');
                            $("#phone_errors").html(resp.phone_error);
                        }
                        if (resp.email_error != '') {
                            $("#email_row").addClass('errors');
                            $("#email_errors").html(resp.email_error);
                        }
                        if (resp.from_country_error != '') {
                            $("#from_country_row").addClass('errors');
                            $("#from_country_errors").html(resp.from_country_error);
                        }
                        if (resp.to_country_error != '') {
                            $("#to_country_row").addClass('errors');
                            $("#to_country_errors").html(resp.to_country_error);
                        }
                        if (resp.from_city_error != '') {
                            $("#from_city_row").addClass('errors');
                            $("#from_city_errors").html(resp.from_city_error);
                        }
                        if (resp.to_city_error != '') {
                            $("#to_city_row").addClass('errors');
                            $("#to_city_errors").html(resp.to_city_error);
                        }
                        if (resp.departure_date_error != '') {
                            $("#departure_date_row").addClass('errors');
                            $("#departure_date_errors").html(resp.departure_date_error);
                        }
                        if (resp.return_date_error != '') {
                            $("#return_date_row").addClass('errors');
                            $("#return_date_errors").html(resp.return_date_error);
                        }
                        if (resp.number_of_passengers_error != '') {
                            $("#number_of_passengers_row").addClass('errors');
                            $("#number_of_passengers_errors").html(resp.number_of_passengers_error);
                        }
                    } else {
                        // No errors. Rewrite the category list.
                        $("#categories").fadeTo('fast', 0);
                        var text = new String();
                        for(i=0; i<resp.quotes.length ;i++){
                            var m = resp.quotes[i]
                            text += "<li>" + m + "</li>"
                        }
                        $("#categories").html(text);
                        $("#categories").fadeTo('slow', 1);
                        $("#id_trip_type").val("");
                        $("#id_company_name").val("");
                        $("#id_individual_name").val("");
                        $("#id_phone").val("");
                        $("#id_email").val("");
                        $("#id_from_country").val("");
                        $("#id_to_country").val("");
                        $("#id_from_city").val("");
                        $("#id_to_city").val("");
                        $("#id_departure_date").val("");
                        $("#id_return_date").val("");
                        $("#id_number_of_passengers").val("");
                    }
                    // Always show the message and re-enable the form.
                    $("#message").html(resp.message);
                    $("#message").removeClass('hide');
                    $("#add_cat").fadeTo('slow', 1);
                    $("#add_cat_btn").removeAttr("disabled");//attr('disabled', '');

            // Set the Return data type to "json".
            }, "json");
            return false;
        });

    });
    </script>
๐Ÿ‘คRadek

Leave a comment