Results 1 to 10 of 10

Thread: [PHP] Checking if a form has been submitted, the correct way...

  1. #1

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Resolved [PHP] Checking if a form has been submitted, the correct way...

    Checking that a form has been submitted is one of the first things you will encounter when handling form inputs. For arguements sake, here is my example form:

    Code:
    <form action="script.php" method="post">
       <input name="email" type="text" />
       <input name="submit" type="submit" value="Submit">
    </form>

    What the form above will do is post the variables email and submit to the action, script.php.

    At this stage most people tend to check to see if a form has been executed by verifying if the submit button variable has been delivered to the receiving script, using code similar to this:

    PHP Code:
    if(isset($_POST['submit'])) 
    In most cases this will work, but there are a few occasions when it won't. To identify when this condition will fail we have to look at the different ways of submitting a form.

    There are two ways of submitting a form, one is to click the submit button, the other is to hit return, of which will envoke the submit button. The problem occurs when you hit the return button (as most people do), when using Internet Explorer (the most commonly used browser) and when the submit button does not have focus. This will submit the form, but it will not send the submit variable, which would mean the script we are using above will fail.

    Despite this flaw, there is an easy solution:

    Code:
    <input type='hidden' name='submit' />
    Here we are setting a hidden variable within our form, so no matter how the form is submitted, the submit variable will always be posted, this means there is no requirement to modify how your PHP works.


    I hope this helps,
    ILMV


    TODO:
    - Create demonstration script

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [PHP] Checking if a form has been submitted, the correct way...

    not to step on your toes, but I believe (and please, correct me if I'm wrong) that a better way of checking whether or not a form has been submitted is to check the request method.
    PHP Code:
    if($_SERVER['REQUEST_METHOD'] == "POST"

  3. #3

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: [PHP] Checking if a form has been submitted, the correct way...

    Blast , never realised that one existed.

    It still demonstrates that my initial method is the incorrect way of checking if a form has been submitted, changing it to the way you suggest would fix this problem.

    Many thanks Kows!

  4. #4
    Addicted Member
    Join Date
    Mar 2007
    Location
    India
    Posts
    227

    Re: [PHP] Checking if a form has been submitted, the correct way...

    Thanks both of you for such a simple solution.

  5. #5
    New Member
    Join Date
    Aug 2011
    Posts
    13

    Re: [PHP] Checking if a form has been submitted, the correct way...

    agree)) simple and quick solution..good job guys!! personally i've never done it on my own

  6. #6
    New Member
    Join Date
    Nov 2012
    Posts
    2

    Re: [PHP] Checking if a form has been submitted, the correct way...

    Quote Originally Posted by kows View Post
    not to step on your toes, but I believe (and please, correct me if I'm wrong) that a better way of checking whether or not a form has been submitted is to check the request method.
    PHP Code:
    if($_SERVER['REQUEST_METHOD'] == "POST"
    This won't work if the form submitted uses the GET method. The best way, unfortunately, is to add what I_Love_My_Vans suggested, add a <input type='hidden' name='submit' /> to EVERY form page. Kind of sucks, but I don't think there's a better way yet

  7. #7
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [PHP] Checking if a form has been submitted, the correct way...

    Quote Originally Posted by charles254 View Post
    This won't work if the form submitted uses the GET method. The best way, unfortunately, is to add what I_Love_My_Vans suggested, add a <input type='hidden' name='submit' /> to EVERY form page. Kind of sucks, but I don't think there's a better way yet
    Well, using the Get method is the incorrect way of doing things and it is not very secure.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  8. #8
    New Member
    Join Date
    Nov 2012
    Posts
    2

    Re: [PHP] Checking if a form has been submitted, the correct way...

    Quote Originally Posted by Nightwalker83 View Post
    Well, using the Get method is the incorrect way of doing things and it is not very secure.
    "incorrect way of doing things". Not to get into a philosophical battle but it really depends on what you're doing. If you're filling out a form to fetch a result set from a db, you would/should be using GET over POST and the if($_SERVER['REQUEST_METHOD'] == "POST") would not work for all ways a form can be submitted. The hidden field method would cover all cases.

  9. #9
    New Member
    Join Date
    Dec 2012
    Posts
    1

    Resolved Re: [PHP] Checking if a form has been submitted, the correct way...

    I find that $_SERVER['REQUEST_METHOD'] is sometimes set to GET or POST even when there has been no form submitted for that particular page, so I wouldn't advise using that method.
    Last edited by kkhugs; Dec 29th, 2012 at 07:57 PM.

  10. #10
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: [PHP] Checking if a form has been submitted, the correct way...

    Quote Originally Posted by I_Love_My_Vans View Post

    PHP Code:
    if(isset($_POST['submit'])) 
    There are two ways of submitting a form, one is to click the submit button, the other is to hit return, of which will envoke the submit button. The problem occurs when you hit the return button (as most people do), when using Internet Explorer (the most commonly used browser) and when the submit button does not have focus. This will submit the form, but it will not send the submit variable, which would mean the script we are using above will fail.
    @love_vans
    As I am new to web development, I think i need little more explanation. Your above-mentioned text I made it bold, is little not picking to my mind. If you please little explain it to me so I would grasp it properly. That why this way is not working everywhere.



    Despite this flaw, there is an easy solution:

    Code:
    <input type='hidden' name='submit' />
    Here we are setting a hidden variable within our form, so no matter how the form is submitted, the submit variable will always be posted, this means there is no requirement to modify how your PHP works.
    I need little more explanation on this hidden field too. I found this code in so many code snippets even in books. But non of them could explain it that how thing actually works.

    Thanks a lot if you could provide me this detail.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width