javascript - Submit multiple forms same jquery script -
javascript - Submit multiple forms same jquery script -
i have multiple forms , want of them processed single jquery script, of course of study have php functions work correctly, tried them separately.
this script:
function proceso_form(type_form, id_div_error){ var url = "my_url.php?form="+type_form; //functions var response = document.getelementbyid(id_div_error); response.innerhtml="<img src='img/loader.gif' style='margin-right: 5px;'/>loading ..."; // response.style.display='block'; $.ajax({ type: "post", url: url, data: $(this).serialize(), //id form success: function(data) { if (data==1){ window.location.reload(); }else{ response.innerhtml=data; // show php response. } } }); homecoming false; };
my form looks this
<form id="contacto" name="contacto" method="post" onsubmit="proceso_form('contacto', 'cargando')"> <input type="text" name="name"class="form-control"> <input type="text" name="phone" class="form-control"> <input type="email" name="email" class="form-control"> <textarea style="height:100px;margin-bottom:0px" name="messaje" class="form-control"></textarea> <input style="margin-top:5px" type="submit" class="btn btn-block" value="send"> </form>
i think problem can't set script in onsubmit
, have no idea.
your html must like
<form id="contacto" name="contacto" method="post" onsubmit="return proceso_form(this, 'cargando')"> ... </form>
and within function:
function proceso_form(form, id_div_error){ var $form = $(form); var url = "my_url.php?form="+$form.attr('id'); //functions var response = document.getelementbyid(id_div_error); response.innerhtml="<img src='img/loader.gif' style='margin-right: 5px;'/>loading ..."; // response.style.display='block'; $.ajax({ type: "post", url: url, data: $form.serialize(), //id form success: function(data) { if (data==1){ window.location.reload(); }else{ response.innerhtml=data; // show php response. } } }); homecoming false; };
by passing this
function passing whole form reference.
hope help.
javascript php jquery html ajax
Comments
Post a Comment