DaCruzR
DaCruzR

Reputation: 407

Disable form submit button using javascript

Basically I have a form inside a php file and I want to disable the submit button once a user clicks submit. If not my database gets spammed after multiple clicks.

I have searched stackoverflow & googled my problem and tried a lot of solutions, not working. Not sure if I am being stupid or what. After numerous trial and error still can not get the form to submit that's why I made a new thread. I have tried setting the "onsubmit" property on the submit input of the form but the form does not submit at all, just stuck. Also tried other Javascript solutions without success.

Any help would be greatly appreciated! Please do not be harsh, I have little to no experience in Javascript.

<form action="adduser.php" method="POST">
              <div class="form_attr">
                <label>Username:</label><input type="text" name="username" value="<?php if ($err_flag == true){echo htmlspecialchars($username);}?>">
                <br/>
                <label>Password:</label><input type="password" name="password" value="">
                <br/>
                <label>Firstname:</label><input type="text" name="firstname" value="<?php if ($err_flag == true){echo htmlspecialchars($firstname);}?>">
                <br/>
                <input type="checkbox" name="admin" value="1"> Admin<br/>
                <input type="checkbox" name="receptionist" value="2"> Receptionist<br/>
                <input type="checkbox" name="technician" value="4"> Technician<br/>
                <input type="submit" name="submit" value="submit">
              </div>
           </form>

Upvotes: 4

Views: 110

Answers (2)

rahul
rahul

Reputation: 187030

Something like this should help.

$(function(){
    $("input[type='submit'][name='submit']").click(function(){
        $(this).attr("disabled", "disabled");
        return true;
    });
});

Demo

Upvotes: 2

inf1ux
inf1ux

Reputation: 283

<input type="submit"  onclick="this.disabled=true;" name="submit" value="submit" >

Did not test it, but it should work.

Note: This is not a good solution - someone can disable js and spam you. You need to prevent this in the backend(in php).

Upvotes: 1

Related Questions