Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP $_POST problems

    My PHP code relies very heavily on $_POST variable data. It generates a form which when submitted, reloads the same PHP page according to what $_POST data the user has entered. The problem is it needs the data to know what to generate, and when you visit the page for the first time not having submitted data, it doesn't display what it should. Is there any way I can have default values for my $_POST variables so it will show correctly the first time you visit? I've already tried the following and they don't seem to work...

    $spec = $_POST["spec"];
    if ($spec = Null) {$spec = "";}

    or

    $spec = $_POST["spec"];
    if ($spec = Undefined) {$spec = "";}

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Use the isset() function. You should only have to test one $_POST variable, since if one is set, it's safe to assume the form has been submitted and they're all set.

    PHP Code:
    if (!isset($_POST['submitBtn'])) {
        
    //do the stuff you'd do the first time on the page
    } else {
        
    //do the stuff you'd do once the form has been submitted


  • #3
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah... isset will do

  • #4
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    or empty()?

  • #5
    Regular Coder
    Join Date
    Dec 2007
    Location
    Nebraska
    Posts
    113
    Thanks
    0
    Thanked 2 Times in 2 Posts
    empty will handle if the variable is not set and if it is blank. Watch out for values that can validly be 0 however. that also evaluates to empty. You could also check the request_method to determine if it was a post.
    Deliver yesterday, code today, think tomorrow.

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Quote Originally Posted by rafiki View Post
    or empty()?
    Don't use empty(). If you do, you are automatically saying that all fields are required. In some cases this might work, but it's just silly to use this. Just use isset().

  • #7
    Regular Coder
    Join Date
    Dec 2007
    Location
    Nebraska
    Posts
    113
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by aedrin View Post
    Don't use empty(). If you do, you are automatically saying that all fields are required. In some cases this might work, but it's just silly to use this. Just use isset().
    If you use empty on the entire $_POST variable, it will return true if $_POST is not set, is null or if there are no elements in the post array. If you use it on a single value it will return true if the varaible is not set, null or if it is an empty string or 0. It will not return true if you have a 20 element array where each element is blank. That doesn't constitute all fields required.

    http://us3.php.net/empty
    Deliver yesterday, code today, think tomorrow.

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Who suggested that it should be used like that?

  • #9
    Regular Coder
    Join Date
    Dec 2007
    Location
    Nebraska
    Posts
    113
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by aedrin View Post
    Who suggested that it should be used like that?
    Quote Originally Posted by aedrin
    Don't use empty(). If you do, you are automatically saying that all fields are required.
    That statement is incorrect. How can the use of empty in this way...

    PHP Code:
    if (empty($_POST['submitBtn'])) { 
        
    //do the stuff you'd do the first time on the page 
    } else { 
        
    //do the stuff you'd do once the form has been submitted 

    require all fields? The OP simply wants to know if the request is post or not. He wasn't asking about validation.
    Deliver yesterday, code today, think tomorrow.

  • #10
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Quote Originally Posted by hammer65 View Post
    The OP simply wants to know if the request is post or not. He wasn't asking about validation.
    Correct.

    Hence using empty() would impose another requirement (that the field have content when submitted) on the field. Which is (as you said) not what the OP requested. Besides the issue that 0 is also considered empty (it's not hard to imagine a form that requires 0 as its input).

    require all fields?
    Obviously I am not implying that using it once will require all fields. If the OP would have replaced all isset() calls with empty(), then all fields would be required.

    Not sure how my statement is incorrect in that sense.

  • #11
    Regular Coder
    Join Date
    Dec 2007
    Location
    Nebraska
    Posts
    113
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by aedrin View Post
    Correct.

    Hence using empty() would impose another requirement (that the field have content when submitted) on the field. Which is (as you said) not what the OP requested. Besides the issue that 0 is also considered empty (it's not hard to imagine a form that requires 0 as its input).



    Obviously I am not implying that using it once will require all fields. If the OP would have replaced all isset() calls with empty(), then all fields would be required.

    Not sure how my statement is incorrect in that sense.
    You did notice that the value being checked is called, "submitBtn" did you not? Most of the time you just see.

    PHP Code:
    if($_POST['submit'])
    {
       
    // we have a form submission because the button value is there

    The value of the submit button should be known. If the value isn't what is expected then someone is monkeying with the form.
    Deliver yesterday, code today, think tomorrow.

  • #12
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    You did notice that the value being checked is called, "submitBtn" did you not?
    No, I did not. I didn't think someone would have 2 versions of a form to cover the initial load. The initial example didn't really suggest what was done in either branch though. So that actually makes it more confusing of an answer.

    Most of the time you just see.
    Not sure what you mean by this though.

  • #13
    Regular Coder
    Join Date
    Dec 2007
    Location
    Nebraska
    Posts
    113
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by aedrin View Post
    Not sure what you mean by this though.
    I normally don't use the method but most of the time I have seen that method used to detect if the form is being submitted or if it is first visit.
    Deliver yesterday, code today, think tomorrow.


  •  

    Posting Permissions

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