ginerjm Posted September 14, 2012 Share Posted September 14, 2012 OK - I've done this before, but I'm having a problem now. I have a form with a submit button declared as <input type="submit" name="btn" value="Log In" onclick="return handleLogin('LogIn')"> The onclick routine - after much debugging - is working and at the end of its processing I have this: allText = myreq.responseText; rtnval = handleResponse(allText); if (rtnval) { alert("returniing to button with true"); return true; } else { alert("returniing to button with false"); return false; } The alerts are displaying that the return value is "false", yet my original button performs the submit, which I know because the script in the form's action is being called - when it shouldn't be. I hope I've provided enough info - does anyone see what I'm not? Quote Link to comment https://forums.phpfreaks.com/topic/268376-onclick-not-suppressing-the-submit/ Share on other sites More sharing options...
lemmin Posted September 14, 2012 Share Posted September 14, 2012 I found the error. You have an extra 'i' in "returning!" Just kidding. Try attaching that function to the onsubmit event of your form instead of the submit button: <form onsubmit="return handleLogin('LogIn')"> [code] Quote Link to comment https://forums.phpfreaks.com/topic/268376-onclick-not-suppressing-the-submit/#findComment-1377919 Share on other sites More sharing options...
ginerjm Posted September 17, 2012 Author Share Posted September 17, 2012 Nice idea, but I need to know which 'submit' button was clicked. Quote Link to comment https://forums.phpfreaks.com/topic/268376-onclick-not-suppressing-the-submit/#findComment-1378606 Share on other sites More sharing options...
Adam Posted September 17, 2012 Share Posted September 17, 2012 Are the return statements within a callback? Can you show the whole code please. Quote Link to comment https://forums.phpfreaks.com/topic/268376-onclick-not-suppressing-the-submit/#findComment-1378623 Share on other sites More sharing options...
ginerjm Posted September 17, 2012 Author Share Posted September 17, 2012 This is the js that is called from the submit buttons. function handleLogin(btn) { var uid = document.getElementById('uid').value; if (uid=='') { alert("You must enter a name"); return false; } var pswd = document.getElementById('pswd').value; if (pswd=='') { alert("You must provide a password"); return false; } /* Now do an xmlhttprequest to validate the credentials Returned value will be similar to above returned values */ url="http://jimginer.net/bowling_login.php?uid="+uid+"&pswd="+pswd+"&btn="+btn; alert("in js_login_test.php calling bowling_login via xml"); /* create a var to do the process */ var myreq = new XMLHttpRequest(); myreq.open("GET", url, true); myreq.send(null); myreq.onreadystatechange = function() { if (myreq.readyState == 4) { if (myreq.status == 200) { /* handle the data returned from the above GET request */ allText = myreq.responseText; rtnval = handleResponse(allText); if (rtnval) { alert("returniing to button with true"); return true; } else { alert("returniing to button with false"); return false; } } else { alert("Error processing login attempt - message follows: "+myreq.statusText); return false } } else { } } } The buttons that call this function look like: <input type="submit" name="btn" value="Log In" onclick="return handleLogin('LogIn')"> <input type="submit" name="btn" value="Register" onclick="return handleLogin('Register')"> As you can see my alerts say one thing, but my results are different. Quote Link to comment https://forums.phpfreaks.com/topic/268376-onclick-not-suppressing-the-submit/#findComment-1378705 Share on other sites More sharing options...
ginerjm Posted September 17, 2012 Author Share Posted September 17, 2012 ok - it is solved. My return was created from a look at a string result. I did not properly receive the string value of "true" or "false". I changed it to properly convert it to Boolean and now I get a proper return. Quote Link to comment https://forums.phpfreaks.com/topic/268376-onclick-not-suppressing-the-submit/#findComment-1378711 Share on other sites More sharing options...
ginerjm Posted September 17, 2012 Author Share Posted September 17, 2012 I take it back!! One test and I thought I had it, but alas... NOT! So - any ideas from the code I last submitted? Quote Link to comment https://forums.phpfreaks.com/topic/268376-onclick-not-suppressing-the-submit/#findComment-1378714 Share on other sites More sharing options...
Adam Posted September 17, 2012 Share Posted September 17, 2012 You're only returning from the anonymous function assigned to the state change event, not the actual handleLogin() function. You need to always return false from handleLogin(), regardless of what happens with the request. Remember that the callback you assign to the XHR object isn't called for a relatively long time; handleLogin() has long since returned by that point. Quote Link to comment https://forums.phpfreaks.com/topic/268376-onclick-not-suppressing-the-submit/#findComment-1378756 Share on other sites More sharing options...
ginerjm Posted September 18, 2012 Author Share Posted September 18, 2012 I never realized that was how this worked. It's code I lifted and have implemented 2-3 times successfully. Apparently I should re-visit those places as well. So - add a return false after the anonymouse function's place? And so how does the result of the anonymous function call then get 'handled'? Quote Link to comment https://forums.phpfreaks.com/topic/268376-onclick-not-suppressing-the-submit/#findComment-1378890 Share on other sites More sharing options...
ginerjm Posted September 18, 2012 Author Share Posted September 18, 2012 Ok - Here's what I added: . . . var myreq = new XMLHttpRequest(); myreq.open("GET", url, true); myreq.send(null); rtnval=false; myreq.onreadystatechange = function() { if (myreq.readyState == 4) { if (myreq.status == 200) { /* handle the data returned from the above GET request */ allText = myreq.responseText; rtnval = handleResponse(allText); if (rtnval) alert("Got a response - returning "+rtnval); return rtnval; } else { alert("Error processing login attempt - message follows: "+myreq.statusText); return false; } } } return false; /* always return false from this function */ where I added the vary last line, which is now the last of my handleLogin function. So my alert shows that the user has entered valid credentials and that the anon func is about to return a true value, but my submit never happens now - so I guess the return false is working well. Are you sure that's how to end that function? Quote Link to comment https://forums.phpfreaks.com/topic/268376-onclick-not-suppressing-the-submit/#findComment-1378892 Share on other sites More sharing options...
Adam Posted September 18, 2012 Share Posted September 18, 2012 The result of the AJAX request should not determine whether the handleLogin() function returns true or false. The return statement is simply to stop propagation. Even if the request fails, we don't want the form to then go on and submit. Are you sure that's how to end that function? Yeah, although nothing will be waiting for the return values of the anonymous function, so you may as well remove them. Quote Link to comment https://forums.phpfreaks.com/topic/268376-onclick-not-suppressing-the-submit/#findComment-1378897 Share on other sites More sharing options...
ginerjm Posted September 18, 2012 Author Share Posted September 18, 2012 The whole concept here is to let the onclick function call (handleLogin) determine whether my html form is submitted. Quote Link to comment https://forums.phpfreaks.com/topic/268376-onclick-not-suppressing-the-submit/#findComment-1378901 Share on other sites More sharing options...
Adam Posted September 18, 2012 Share Posted September 18, 2012 Bit odd, but okay. In the anonymous function then you will need to manually trigger the form submit (if needed): document.formName.submit(); Quote Link to comment https://forums.phpfreaks.com/topic/268376-onclick-not-suppressing-the-submit/#findComment-1378922 Share on other sites More sharing options...
ginerjm Posted September 18, 2012 Author Share Posted September 18, 2012 Yes - so I figured. A whole new view of the httprequest usage. Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/268376-onclick-not-suppressing-the-submit/#findComment-1378923 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.