4

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>
1

2 Answers 2

2

Something like this should help.

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

Demo

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

2 Comments

Thanks, I've tried your solution I can get the button to disable but the form doesn't post. Any ideas?
no errors with your code, however after some searching I found a solution that works! stackoverflow.com/questions/6634999/…
1
<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).

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.