1

so this might be something super simple to do, but I can't seem to get it. I have a form and a validation for it. The validation text works. If I use a normal submit button the form works correctly, but if I use javascript to submit the form, it does not work 100%.

<form id="transport" name="transport" method="post" action="submit_trans_request.php">
    <input type="text" name="f_name" value="<?php echo htmlspecialchars($row['f_name'], ENT_QUOTES); ?>" style="font-size:16px;"  />
    <a href="javascript:onclick=validateForm(); document.transport.submit();" class="submit_btn">Submit</a>

So basically I am looking for a way to stop the submit if validateForm() returns an alert. Not sure how I go about that.

Any help is greatly appreciated. Thank you in advance!

3
  • You need return false; Commented Oct 10, 2016 at 20:04
  • Where do I put that? Commented Oct 10, 2016 at 20:06
  • Where you put your alert, inside the block where you have decided the form didn't validate. Commented Oct 10, 2016 at 20:08

1 Answer 1

2

You could do it the way you've designed it by just returning false in the validationForm().

If I were you, I would just call whatever method you want when the user clicks submit, then inside of that method, call the validationForm() method. If that returns true, go forward with the process, otherwise alert the user.

Something like this:

<form>
   <input type="text" />
   <a href="javascript:onclick=submitData();"/>
</form>

Then in the submitData() function, do something like this:

function submitData() {
    var b = validationForm();

    if (b) {
        // submit data
    } else {
        // alert user something entered wrong
    }
}

Now, in the validationForm() function, you need to make sure that you return true or false.

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

1 Comment

As I thought, I overthink things and make them more complex than they should be. I appreciate all the help! Thank you!

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.