2

On my site I both have a login form, and a form for something else (this I all the second form)

They can both be present on the same page

How do I check if the second form is submitted?

I have learned that using this is good practice:

if (strtoupper($_SERVER['REQUEST_METHOD']) === 'POST') { } 

But this would also be true if the login form is submitted

How do I target (check if submitted) the second form and disregard the loginform? Should I check by it's name?

5
  • Check the name of submit button. Commented Jun 28, 2014 at 12:49
  • The method you are showing us just check what is the request method of HTTP is. Commented Jun 28, 2014 at 12:51
  • @u_mulder Hm, will that work if people press ENTER? Commented Jun 28, 2014 at 12:53
  • @RahilWazir I know.. hence my question Commented Jun 28, 2014 at 12:53
  • You could argue that checking the request method is bad practise, as you might change a form POST to a GET and then you've got a problem. Better to check for named parameters in the $_REQUEST array. Commented Jun 28, 2014 at 12:57

1 Answer 1

5

You can either have the forms have a different action, e.g. append ?action=login on the login form, or you can name the submit buttons something different on each form, e.g. <input type="submit" name="login" />. That way you can check isset($_REQUEST['login']) to see if it's the login form.

If a submit button is named in a form, it will be included in the POST however the form is submitted even if enter is pressed. If the form is sent via POST, then you can still include GET parameters in the form's action attribute, so either of these methods should do the job.

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

3 Comments

I think I'll go with targeting the form action. The forms both already have different subpage actions. So I just check that. Thanks.
Another alternative is to have your login handler on a completely different URL, so that it's handled by a different script.
Yes, It's already set up like that. Just needed an extra division of the too forms

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.