javascript - Issue with form Submit - information to database and redirect to thank you page -
issue form submit - information need sent database , need redirect thank page
here code , fiddle link well. thanks.
i trying redirect - thank page
instead redirected - "action"- url
with existing code, there no issue of sending information database. works fine, not redirecting me thank page.
html:
<form name="mailinglist" method="post" id="formid" action="url" onsubmit="return validate_form(this);"> <input type="email" name="emailaddress" placeholder="email address" maxlength="100" size="28"> <br> <input type="submit" name="submit" value="submit" width="260px"> </form> var frmvalidator = new validator("mailinglist"); frmvalidator.addvalidation("email", "maxlen=50"); frmvalidator.addvalidation("email", "req"); frmvalidator.addvalidation("email", "email"); <script> jquery(function () { jquery('#subscribe').click(function () { jquery.ajax({ type: 'post', url: 'url', data: 'emailaddress=' + jquery('input[name="emailaddress"]').val(), success: function (data) { if (jquery(data).find('.subscribe_valid').length) { location.href = 'thank page'; } }, error: function () { alert('error handling here'); } }); }); }); </script>
prerequisites
form , script needs live @ same origin ajax target - if not, no need read further - example can never test teabeyond jsfiddle.
jquery has loaded before script
validation has inside jquery load event handler
then need cancel form submission with
....submit(function(e) { e.preventdefault();
here code attempting fix original question. expect serialised form rather context in ajax
note jsfiddle cannot ajax site jsfiddle code not work @ jsfiddle
function validate_form(theform) { if (theform.emailaddress.value=="") { alert("please enter email"); return false; } return true; } $(function () { $('#formid').submit(function (e) { e.preventdefault(); // cancel form if (validate_form(this)) { // form's action must point page on same origin form $.post(this.action, $(this).serialize(), function () { window.location.replace("https://www.othersite.com/articles.asp?id=252"); }); } }); });
instead of $(this).serialize(),
can have
{"emailaddress2",this.emailaddress2.value}
Comments
Post a Comment