0

I have this problem:

my HTML code:

<form name="addUser" method="post" action="../src/process_register.php">
    ....
    <button type="submit" onClick="formhash_register(this.form);">Submit</button>
</form>

my JavaScript code:

function formhash_register(form) 
{
var username = form.inputUsername.value;

if(username == "")
        return false;
    else
        form.submit();
}

Although my function returns false, you will go to another page, and thats not what I wanted. Is there way to cancel submit process when the submit button is pressed ?

2 Answers 2

4

Try this:

 <button type="submit" 
      onClick="return formhash_register(this.form);">Submit</button>
Sign up to request clarification or add additional context in comments.

1 Comment

Just have in mind that javascript can be disabled, so preventing submission is not 100% bulletproof. Always validate user input on server too.
0

Change type to "button" which shouldn't automatically submit this form.

<button type="submit" onClick="formhash_register(this.form);">Submit</button>

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.