javascript - jQuery form submitting isn't submitting the form -
i got basic html form validate through jquery,
this 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 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 character don't understand, it's language.
as can see, did prevent form being submitted, had use selector "form" or otherwise, won't work reason >_<. therefore, tried using selector "form" , worked.
now, can see in last if condition, want submit form if variable "error" empty, form doesnt submit. know how make form submit :-)
thanks in advance!
the purpose of e.preventdefault()
stop element's default action. in form.submit(), prevents form being submitted.
move preventdefault() here:
if(error != ""){ $("#form_errors").html(error).hide().slidedown(500); e.preventdefault(); //do not want submit if there errors }else{ $("#form_errors").slideup(500); $("#add_product").submit(); }
Comments
Post a Comment