Jump to content

Posting Form after OnClick event


JasonHarper

Recommended Posts

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
}

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.