#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171

    Has form been submitted?


    What's the best way to see if the form has been submitted or not?
    PHP Code:
    if(isset($_POST))
        {
            
        }
    if(
    $_POST)
        {
            
        }
    if(isset(
    $_POST['some_input']))
        {
            
        }
    if(
    $_POST['some_input'])
        {
            
        } 
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,998
    Rep Power
    9397
    isset($_POST) is useless.

    $_POST can work but it only tells you some kind of data was submitted.

    isset($_POST['some_input']) tells you whether it was present in the request.

    $_POST['some_input'] will raise warnings if it was not present.

    Ideally you would test that all form inputs you're expecting are present. Typically one just looks for a submit button or a particular required bit of information. The REQUEST_METHOD is another option but has the same problem as checking $_POST.
    Last edited by requinix; July 12th, 2013 at 03:27 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    Depending on the purpose of the form, I may do something similar to:
    PHP Code:
    if(isset($_POST['action'])) {
      switch (
    $_POST['action']) {
          case 
    "Add":
              
    // Process to add to DB
              
    break;
          case 
    "Edit":
              
    // Process to edit in DB
              
    break;
          case 
    "Remove":
              
    // Process to remove from DB
              
    break;
      }
    } else {
      
    // Your form...

    And if your wish to run a process and send them to a Congradulations page upon success, you can place redirects after your proccesses to other pages, or meerly back to this page with a case built for success.
    Last edited by Triple_Nothing; July 12th, 2013 at 12:45 AM.
  6. #4
  7. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    I hate it but I've fallen into the habit of including a hidden "action" form field with many of my forms, then looking for it similar to the last example. Though I rarely handle multiple actions in one Controller--I create a Controller for each instead.

    So "action" is just there to tell PHP that, "Yes, this form was submitted". You can then check from there that the expected form values exist, validate them, etc.

    It also supports a consistent practice.

    Sometimes you might submit a form via Javascript and not have a submit button, but can still have a hidden field. Sometimes you'll trigger an AJAX post and you can just include an "action" parameter. Then you might have more actions than GET, POST, etc. can represent, so relying on something YOU control is more flexible. (Yeah, Jacques, I went there again...)

    So accepting the "action" parameter in PHP maximizes flexibility, IMO.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    195
    Rep Power
    45
    Not sure if this is good practice or not, but this is how I do mine:

    PHP Code:
    if ($_SERVER['REQUEST_METHOD'] != 'POST'){
        
    $action $_SERVER['PHP_SELF'];
    }
    else {
            
    //process form submission//

  10. #6
  11. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,998
    Rep Power
    9397
    Originally Posted by BlackAce
    Not sure if this is good practice or not, but this is how I do mine:

    PHP Code:
    if ($_SERVER['REQUEST_METHOD'] != 'POST'){
        
    $action $_SERVER['PHP_SELF'];
    }
    else {
            
    //process form submission//

    As long as you keep in mind that PHP_SELF is insecure and vulnerable to XSS, that can work fine.
  12. #7
  13. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by BlackAce
    Not sure if this is good practice or not, but this is how I do mine:

    PHP Code:
    if ($_SERVER['REQUEST_METHOD'] != 'POST'){
        
    $action $_SERVER['PHP_SELF'];
    }
    else {
            
    //process form submission//

    You could also test count($_POST).
    It's not quite identical to what you have--someone could POST with no variables--but you might want the same outcome, anyhow.

IMN logo majestic logo threadwatch logo seochat tools logo