javascript - Form submission determiend on Ajax response -
there's form users fill out , click submit button. data gets sent server , based on servers server's response form submitted or not (and page redirected or not). problem form submits , page redirects before server's response received.
function submitform(name, data1) { var result; var checkresult = function(result) { alert("final value is: "+result) return result } var xmlhttp if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if(xmlhttp.readystate == 4 && xmlhttp.status == 200) { if(xmlhttp.responsetext) { result = true } else { document.getelementbyid("report").innerhtml = "blah blah blah" result = false } checkresult(result) } } xmlhttp.open("post", "checkdata1.php", true); xmlhttp.setrequestheader("content-type", "application/x-www-form-urlencoded") xmlhttp.send("name="+encodeuricomponent(name.value)+"&data1="+encodeuricomponent(data1.value)) //return false }
here's html form calls above javascript function.
<form onsubmit="return ispasswordcorrect(document.getelementbyid('name'), document.getelementbyid('txtfield1'))" action="nextpage.php" method="get">
i know idea function return false
prevent form being submitted, how wait long enough server reply through ajax? way, haven't needed jquery , if it's not necessary i'd prefer not start using now.
i use <button>
doesn't submit form (the type isn't "submit"
):
<form ...> ... <button onclick="submitform(...)">submit</button> </form>
then @ end of submitform
, can put:
document.queryselector("form").submit();
when want submit form.
Comments
Post a Comment