Ricardo Mehr
Ricardo Mehr

Reputation: 310

Disable button after submit with form that has PHP function. No jQuery

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?

Upvotes: 0

Views: 261

Answers (2)

Chaminda Chanaka
Chaminda Chanaka

Reputation: 190

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>

Upvotes: 1

Gotenks-J
Gotenks-J

Reputation: 79

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

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

Upvotes: 0

Related Questions