0

I have a HTML form echoed in PHP, the form action calls a PHP function. I want to disable the submit button after its been clicked without using jQuery if possible.

#PHP
echo '
<form method="POST" action="' . phpFunction() . '">
    //form fields
    <button name="submit" type="submit" class="btn btn-primary btn-block">Enviar</button>
</form>';

I have tried using onclick="this.disabled=true" on the button, but it prevents the PHP function from excecuting. What should I do to excecute the function and disable the button?

4
  • I'm curious, what's inside the function? It is just that, the action attribute is just the path to where it will send its data, so there is really no need to make it a function. Commented Nov 28, 2019 at 4:03
  • Just use plain javascript then? If you dont want jQuery :D Commented Nov 28, 2019 at 4:28
  • Sanitization, inserting into DB, but written as a function. Dont know why. Commented Nov 28, 2019 at 4:31
  • after submit set document.getElementById("btnID").disabled = true; it might help Commented Nov 28, 2019 at 5:03

2 Answers 2

1

Use this simple code to disable submit button

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript">
        function form_submission(form_id, button_id) {
            $('#btn_add').attr('disabled', 'disabled');
            document.getElementById(button_id).style.display = "none";
            document.forms[form_id].submit();
        }
    </script>
</head>
<body>
    <form action="submit.php" method="post" name="form_name" id="form_name">
        <input type="text" name="first_name" />
        <input type="text" name="last_name" />
        <input type="button" name="btn_add" id="btn_add" value="save" onclick="form_submission('form_name', 'btn_add')" />
    </form>
</body>

Sign up to request clarification or add additional context in comments.

2 Comments

umm...why would you hide button, the OP asked to disable it.
then don't use hiding part. you can use disable part
0

document.getElementById("yourButtonId").disabled = true;

Taken from here: https://www.w3schools.com/jsref/prop_pushbutton_disabled.asp

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.