Newbie PHP Form Question

Discussion in 'PHP' started by Corwin, Jun 15, 2009.

  1. #1
    I'm only two days old into learning PHP and i've gotten very far - but this one has me stumped.

    On a PHP page that retrieves form data, I have this instruction:

    $userData = $_POST['formData'];

    For error handling if formData isn't present, I figure that, like most every other scripting language I've worked with, I just check to see if $userData is null - however, if formData isn't present the above line kicks out an error of "Undefined index".

    What am I missing? Can someone please tell me how do I check if formData isn't present?
     
    Corwin, Jun 15, 2009 IP
  2. renzmar

    renzmar Peon

    Messages:
    308
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try this:

    if(!empty($_POST['formData'])){
    $userData = $_POST['formData'];
    }

    i hope it will help.. Good luck! :)
     
    renzmar, Jun 15, 2009 IP
  3. kuzmanin

    kuzmanin Peon

    Messages:
    242
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you can use also function
    isset($_POST['formData'])

    or just
    if (trim($_POST['formData']) === '') ...
     
    kuzmanin, Jun 15, 2009 IP
  4. Corwin

    Corwin Well-Known Member

    Messages:
    2,438
    Likes Received:
    107
    Best Answers:
    0
    Trophy Points:
    195
    #4
    Thank You!

    Also, BTW, is this the PHP command like "On Error Resume Next":

    ini_set('display_errors', 0);
     
    Corwin, Jun 15, 2009 IP
  5. Zellach

    Zellach Peon

    Messages:
    107
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    No, you set display_errors to output the errors to the browser screen or not. You want it on when you are debugging, and off when you are in production. Look up try/catch statements to understand error handling in php.
     
    Zellach, Jun 15, 2009 IP
  6. Corwin

    Corwin Well-Known Member

    Messages:
    2,438
    Likes Received:
    107
    Best Answers:
    0
    Trophy Points:
    195
    #6
    Yes, my script works, I am done debugging and the script is now in production.
     
    Corwin, Jun 15, 2009 IP
  7. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #7
    Gray Fox, Jun 16, 2009 IP
  8. Corwin

    Corwin Well-Known Member

    Messages:
    2,438
    Likes Received:
    107
    Best Answers:
    0
    Trophy Points:
    195
    #8
    BTW - the PHP page I've built is called via serverXMLHTTP. So, I want to log errors to a file, but I am also using serverXMLHTTP to check the status of the page and if it is not = 200, I log the error. So I do want the page to return an error status.
     
    Corwin, Jun 16, 2009 IP
  9. bluebenz

    bluebenz Well-Known Member

    Messages:
    876
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    138
    #9
    I was figure the same problem with "undefined index".
    I read that undefined index is for bug checking function for development process and not for live.
     
    bluebenz, Jun 16, 2009 IP
  10. uselessguy

    uselessguy Peon

    Messages:
    52
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Edit php.ini file and update error reporting option

    error_reporting = E_ERROR | E_WARNING | E_PARSE
     
    uselessguy, Jun 17, 2009 IP
  11. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #11
    PHP will not report any error.

    You can simply check this if($_POST['formdata']!="") , because PHP will evaluate $_POST['formdata'] as NULL if it's not set.
     
    techbongo, Jun 17, 2009 IP
  12. Corwin

    Corwin Well-Known Member

    Messages:
    2,438
    Likes Received:
    107
    Best Answers:
    0
    Trophy Points:
    195
    #12
    No, it's throwing an error. I'd rather implement a robust error-checking mechanism on my own, than leave it to chance.
     
    Corwin, Jun 17, 2009 IP