kfirba
kfirba

Reputation: 5441

jQuery form submitting isn't submitting the form

I got a basic HTML form which I validate through jQuery,

this is the form:

<form method="post" action="index.php" enctype="multipart/form-data" id="add_product">
    מק"ט:
    <input type="text" name = "product_cn" value="<?php echo $product_cn;?>" id="cn"/> <span style="margin-right: 20px; color:red; display:none" id="cn_exist"></span>
    <br /> <br />
    שם המוצר:
    <input type="text" name="product_name" value="<?php echo $product_name;?>" id="p_name"/>
    <br /> <br />
    פרטי המוצר:
    <textarea rows = "6" cols = "30" name="product_details"  id="p_details"> <?php echo $product_details;?></textarea>
    <br /> <br />
    מחיר:
    <input type="text" name = "product_price" value="<?php echo $product_price;?>" id="p_price"/>
    <br /> <br />

    תמונה:
    <input type="file" name="fileField" id="p_image" />
    <br /> <br />

    <input type="submit" name="submit" value="רשום מוצר"  />
</form>

this is my validation code:

$("form").submit(function(e){
            e.preventDefault(e);
            var p_cn = $("#cn").val();
            var p_name = $("#p_name").val();
            var p_details = $("#p_details").val();
            var p_price = $("#p_price").val();
            var p_image = $("#p_image").val();

            error = "";
            if(!(p_cn != "") || !(p_cn.length > 0)){
                error += " אנא הוסף הוסף מק\"ט <br /> <br />" ;
            }
            if(!(p_name != "") || !(p_name.length > 0)){
                error += "אנא הזן שם מוצר <br /> <br />";
            }

            if(!(p_price != "") || !(p_name.length > 0)){
                error += " אנא הזמן מחיר. <br /> <br />";
            }


            if(error != ""){
                $("#form_errors").html(error).hide().slideDown(500);
            }else{
                $("#form_errors").slideUp(500);
                $("#add_product").submit();
            }

        });

Please ignore any character you don't understand, it's my language.

As you can see, what I did is prevent the form from being submitted, I had to use the selector "form" or otherwise, it just won't work for some reason >_<. Therefore, I tried using the selector "form" and it worked.

Now, as you can see in the last IF condition, I want to submit the form if the variable "error" is empty, but the form just doesnt submit. I would like to know how to make the form submit :-)

Thanks in advance!

Upvotes: 0

Views: 64

Answers (2)

cssyphus
cssyphus

Reputation: 40038

The purpose of e.preventDefault() is to stop the element's default action. In a form.submit(), it prevents the form from being submitted.

Here is a detailed explanation

Move the preventDefault() to here:

if(error != ""){
    $("#form_errors").html(error).hide().slideDown(500);
    e.preventDefault(); //do NOT want it to submit if there are errors
}else{
    $("#form_errors").slideUp(500);
    $("#add_product").submit();
}

Upvotes: 1

Blender
Blender

Reputation: 298176

e.preventDefault(e); (which should be just e.preventDefault();) stops the submit from happening. You need to do that conditionally:

if (error) {
    e.preventDefault();
} else {
    // No need to prevent the submit
}

Upvotes: 2

Related Questions