2

I'm having a little problem here.
I have a form with a jQuery that disables the submit input when the form is submited.
The problem is: I use PHP to process the data when the input submit is clicked. But apparently the button is being disabled first, then PHP can not perform the processing. I need to find a way to prevent this, take a look in the code snippets:

<form class='send' action='{$_SERVER['PHP_SELF']}' method='post'>
    <!--Form data-->
    <input type='submit' name='finish' value='Send'/>
</form>

jQuery function to disable:

$('.send').on('submit', function()
{
    $(this).find('input[type="submit"]').prop("disabled", true);
});

And then the PHP code to process:

if(isset($_POST['finish']))
{
    //Do things
}

As I said, the PHP don't process the form because JS disabled the button to prevent multiple submitions. How to process PHP before disable the button? Thanks!

5
  • I think the problem is the PHP script never receives the data because you've disabled submission. I assume you're wanting to prevent a refresh? You need to use AJAX. On click, disable the button. On success, let the user know/enable the button again. Commented Apr 29, 2015 at 16:05
  • disabling the input means it's name/value pair won;t be sent. What is importance of it being set in the php? Commented Apr 29, 2015 at 16:07
  • Provided it doesn't violate any specifications you are working towards, you could just hide the button? $(this).find('input[type="submit"]').hide();. I know it's shamelessly q&d, (and no one learns anything). But hey ho Commented Apr 29, 2015 at 16:09
  • Why don you just use preventDefault(); ? i.e.: stackoverflow.com/a/5169572/797495 Commented Apr 29, 2015 at 16:11
  • @PedroLobito I want to disable the button, but after PHP process. Commented Apr 29, 2015 at 16:34

2 Answers 2

2

Since you are disabling the submit button it will not get send over to to server in the $_POST variable so your code doesn't work, as you have said.

Another way to do what you are looking for is to create a hidden HTML input

<input type="hidden" name="form">

Then when you check if the form is send, you will use

if(isset($_POST['form']))
{
    //Do things
}
Sign up to request clarification or add additional context in comments.

5 Comments

I know, but I need to disable after the PHP process.
Can you give us some more information? Why do you need to disable the input and what do you want to achieve?
Well, the rest of my code would be irrelevant and would only make the question extensive. I basically need the PHP to be processed before disabling the button.
I meant why do you need to disable the form after sending the data? When you send the form, you get redirected to the page you specify in the form's action, so doing anything after sending the form is irrelevant. You can always use AJAX to send the form without getting redirected to another page
Thank you for the code and for all information! Hide the input worked great! :)
1

You can solve it easily changing your submit button by a simple button:

<form class='send' action='{$_SERVER['PHP_SELF']}' method='post'>
    <!--Form data-->
    <input type='button' name='finish' value='Send'/>
</form>

$('input[name="finish"]', '.send').on('click', function(){
    $(this).prop('disabled', true);
    $('.send').submit();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.