JasonHarper Posted December 27, 2011 Share Posted December 27, 2011 Hello, When a user clicks a Submit button on my page, I'm using an onClick event to essentially just disable the button and change the message to 'Saving'. However, when I add the onClick code, the form no longer posts using PHP. The button does indeed change but the postback never occurs. Is there something extra I need to add or do? Thank you!! This is my submit button: <input type="submit" id="submit" value="Save Changes" onclick="this.disabled=true; this.value='Saving'">|<a href="/">Cancel</a> Then I have the normal PHP stuff for postback which isn't firing.... //IF POSTBACK if ($_SERVER['REQUEST_METHOD'] == 'POST') { //DO STUFF HERE } Quote Link to comment https://forums.phpfreaks.com/topic/253898-posting-form-after-onclick-event/ Share on other sites More sharing options...
.josh Posted December 27, 2011 Share Posted December 27, 2011 It's not posting because you disabled the button. It's like cutting the wires between the switch and battery and then wondering why the lightbulb doesn't light up when you flip the switch... if you're lookin' to post the form w/out refreshing the page, then use ajax. Quote Link to comment https://forums.phpfreaks.com/topic/253898-posting-form-after-onclick-event/#findComment-1301605 Share on other sites More sharing options...
JasonHarper Posted December 27, 2011 Author Share Posted December 27, 2011 Thanks for the reply! Duh - that makes total sense - not sure what I was thinking. Is there an effective way to disable the button and still post the form? I'm OK with a page refresh.... Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/253898-posting-form-after-onclick-event/#findComment-1301607 Share on other sites More sharing options...
.josh Posted December 27, 2011 Share Posted December 27, 2011 I don't understand why you are trying to disable the button...it's the button that allows the user to submit the form, yes? The button sort of has to work in order for the user to submit the form... Quote Link to comment https://forums.phpfreaks.com/topic/253898-posting-form-after-onclick-event/#findComment-1301608 Share on other sites More sharing options...
JasonHarper Posted December 27, 2011 Author Share Posted December 27, 2011 I'm trying to disable for more of a visual aesthetic...to let the user know something is happening. T he button changes color and says 'Saving' when in a disabled state. On my forms where the submit also processes a credit card transaction, I want to avoid multiple clicks.... Thanks again for the help! Quote Link to comment https://forums.phpfreaks.com/topic/253898-posting-form-after-onclick-event/#findComment-1301610 Share on other sites More sharing options...
.josh Posted December 27, 2011 Share Posted December 27, 2011 Then IMO you should look into using AJAX. Do you use a framework like jQuery? Quote Link to comment https://forums.phpfreaks.com/topic/253898-posting-form-after-onclick-event/#findComment-1301611 Share on other sites More sharing options...
JasonHarper Posted December 27, 2011 Author Share Posted December 27, 2011 OK - thanks! The only jQuery I'm really using is for navigation based stuff. Everything else is all PHP. Quote Link to comment https://forums.phpfreaks.com/topic/253898-posting-form-after-onclick-event/#findComment-1301614 Share on other sites More sharing options...
Philip Posted December 27, 2011 Share Posted December 27, 2011 You'd need to move the JS into a function: <script language="text/javascript"> function fireSubmit() { document.myForm.submit.disabled = true; document.myForm.submit.value = 'Saving...'; document.myForm.submit(); } </script> <form name="myForm"> <input type="text" name="blahText"> <input type="button" value="submit" name="submit" onclick="fireSubmit()"> </form> Untested, but should work (submit button might need to be named something other than submit in order to avoid conflict with the function). Quote Link to comment https://forums.phpfreaks.com/topic/253898-posting-form-after-onclick-event/#findComment-1301616 Share on other sites More sharing options...
JasonHarper Posted December 27, 2011 Author Share Posted December 27, 2011 Thanks KingPhilip! I implemented that code and now the form is indeed posting but the attributes of the button don't seem to be changing. Here is my current code: function fireSubmit() { document.emailNotifications.submitButton.disabled = true; document.emailNotifications.submitButton.value = 'Saving...'; document.emailNotifications.submit(); } </script> <form id="emailNotifications" name="emailNotifications" method="post" action="email-notifications.php"> <input type="submit" id="submitButton" name="submitButton" value="Save Changes" onclick="fireSubmit()"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/253898-posting-form-after-onclick-event/#findComment-1301620 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.