jQuery - AJAX form problem

jQuery - AJAX form problem

Hello, I wanted to make a form where somebody can submit content. I made the form validation with jQuery and now I wanted when the person submits content, the content to be verified if it's already submitted. I made the script and it works well untill some point. I made a variable to help me validate the form. When I validate the form I check every field and if empty I set the variable to false. At the end if the variable is true we can process the form. Now the AJAX part. The script checks if content is already posted then returns 1 (if content already posted) and 0 (if content not posted). At the end of the script I made an if else situation. If returns 1 I set my validation variable to false else set validation variable to true. Here is the problem: I think the variable I set in AJAX success is not available outside of success, and the check for true false to submit the form is done outside.

Here is the validation of the form validation containing the AJAX part:

  1. // Search for empty fields
    $("#submit_content").submit(function() {

    if($("#content_title").val() == '')
    {
    $("#content_title").addClass("empty_field");
    $("#content_title").val("Please complete this required field.");

    formIsValid = false;
    }
    else if($("#content_title").val() == 'Please complete this required field.') {
    formIsValid = false;
    }
    else { formIsValid = true; }



    if($("#content_description").val() == '')
    {
    $("#content_description").addClass("empty_field");
    $("#content_description").val("Please complete this required field.");

    formIsValid = false;
    }
    else if($("#content_description").val() == 'Please complete this required field.') {
    formIsValid = false;
    }
    else { formIsValid = true; }



    if($("input:radio[name=content_category]").is(":checked"))
    {
    $('#msg_content_category').css({ 'display': 'none' });
    formIsValid = true;
    }
    else
    {
    $('#msg_content_category').removeAttr('style');

    formIsValid = false;
    }



    if($("input:radio[name=content_type]").is(":checked"))
    {
    contentType = $("input:radio[name=content_type]:checked").val();

    // alert(contentType);


    $('#msg_content_type').css({ 'display': 'none' });


    if(contentType == 'text')
    {
    if($("#text_content").val() == '')
    {
    $("#text_content").addClass("empty_field");
    $("#text_content").val("Please complete this required field.");

    formIsValid = false;
    }
    else if($("#text_content").val() == 'Please complete this required field.') {
    formIsValid = false;
    }
    else { formIsValid = true; }
    }

    if(contentType == 'image')
    {
    if($("#image_url").val() == '')
    {
    $("#image_url").addClass("empty_field");
    $("#image_url").val("Please complete this required field.");

    formIsValid = false;
    }
    else if($("#image_url").val() == 'Please complete this required field.') {
    formIsValid = false;
    }
    else { formIsValid = true; }
    }

    if(contentType == 'video')
    {
    if($("#video_embed").val() == '')
    {
    $("#video_embed").addClass("empty_field");
    $("#video_embed").val("Please complete this required field.");

    formIsValid = false;
    }
    else if($("#video_embed").val() == 'Please complete this required field.') {
    formIsValid = false;
    }
    else { formIsValid = true; }
    }





    // Check if content already exists
    if(formIsValid == true)
    {
    // Show loading div
    $('#msg_search_load').removeAttr('style');



    // Process PHP file
    $.ajax({
    type: "GET",
    url: "ajax_check_content.php",
    data: "text_content="+$("#text_content").val()+"&image_url="+$("#image_url").val()+"&video_embed"+$("#video_embed").val(),
    cache: false,
    dataType: 'json',
    success: function(html) {

    var checkSubmitted = html.check_submitted;


    if(checkSubmitted == 1)
    {
    // alert("Content already submitted.");
    $('#msg_content_submitted').removeAttr('style');

    formIsValid = false;
    }
    else
    {
    // alert("Content is not submitted.");
    formIsValid = true;
    }


    }
    });



    // Hide loading div
    $('#msg_search_load').css({ 'display': 'none' });
    }



    }
    else
    {
    $('#msg_content_type').removeAttr('style');

    formIsValid = false;
    }



    // Check is form is valid or invalid
    if(formIsValid == false) { return false; }

    });



































































































































































Here is the AJAX part extracted:


  1. // Check if content already exists
    if(formIsValid == true)
    {
    // Show loading div
    $('#msg_search_load').removeAttr('style');



    // Process PHP file
    $.ajax({
    type: "GET",
    url: "ajax_check_content.php",
    data: "text_content="+$("#text_content").val()+"&image_url="+$("#image_url").val()+"&video_embed"+$("#video_embed").val(),
    cache: false,
    dataType: 'json',
    success: function(html) {

    var checkSubmitted = html.check_submitted;


    if(checkSubmitted == 1)
    {
    // alert("Content already submitted.");
    $('#msg_content_submitted').removeAttr('style');

    formIsValid = false;
    }
    else
    {
    // alert("Content is not submitted.");
    formIsValid = true;
    }


    }
    });



    // Hide loading div
    $('#msg_search_load').css({ 'display': 'none' });








































  1.  }

How can I fix this problem? Thank you!