SitePoint Sponsor

User Tag List

Results 1 to 16 of 16

Thread: !$_post

  1. #1
    SitePoint Wizard subnet_rx's Avatar
    Join Date
    Aug 2001
    Location
    Hattiesburg, MS
    Posts
    1,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    !$_post

    I'm trying to grab all values from a form and put them into an array, but it's generating errors if the POST value isn't there. How do I get around this?

  2. #2
    SitePoint Zealot ssttoo's Avatar
    Join Date
    Jan 2004
    Location
    LA, California
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (empty($_POST)) {
        
    // the form is not posted
    } else {
        
    // the form was posted

    this of course assumes that the form is posted, I mean
    HTML Code:
    <form action="some_script.php" method="post">
    A quick way to hide errors is to add a @ before $_POST when you call it, like
    PHP Code:
    $username = @$_POST['username'];
    // or
    $form_data_array = @$_POST

  3. #3
    SitePoint Wizard subnet_rx's Avatar
    Join Date
    Aug 2001
    Location
    Hattiesburg, MS
    Posts
    1,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, the second solution is what I am looking for, the form is being posted, that's my first check, but not all the variables on the form are filled out. So it throws an undefined error

  4. #4
    SitePoint Addict melchiorus's Avatar
    Join Date
    Jun 2004
    Location
    Indiana
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by subnet_rx
    yeah, the second solution is what I am looking for, the form is being posted, that's my first check, but not all the variables on the form are filled out. So it throws an undefined error
    This is due to an option in php.ini which tells it to throw back any notices and warnings. Undefined variables will throw out a notice, but it the script will still function.

    The thing is, if the form has the field and the right name, you couldn't get undefined index errors since the value would be blank, that is $_POST['field'] = '';.

    Check to make sure the field is there and with the correct name. If that doesn't work, try using $HTTP_POST_VARS since shorttags might also be off.
    -Melchior (Stephen Craton)

  5. #5
    SitePoint Wizard subnet_rx's Avatar
    Join Date
    Aug 2001
    Location
    Hattiesburg, MS
    Posts
    1,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the fields are automatically generated from a database based on previous selections, so in fact, the field may not be there. Should I maybe put in hidden values as filler? I thought there would be a much cleaner way to handle it in PHP though.

  6. #6
    SitePoint Addict melchiorus's Avatar
    Join Date
    Jun 2004
    Location
    Indiana
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by subnet_rx
    the fields are automatically generated from a database based on previous selections, so in fact, the field may not be there. Should I maybe put in hidden values as filler? I thought there would be a much cleaner way to handle it in PHP though.
    In that case, just do what ssttoo said. Put the @ sign before the $_POST bit, such as his example:

    PHP Code:
     $username = @$_POST['username'];
    // or
    $form_data_array = @$_POST
    -Melchior (Stephen Craton)

  7. #7
    SitePoint Wizard subnet_rx's Avatar
    Join Date
    Aug 2001
    Location
    Hattiesburg, MS
    Posts
    1,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by melchiorus
    In that case, just do what ssttoo said. Put the @ sign before the $_POST bit, such as his example:

    PHP Code:
     $username = @$_POST['username'];
    // or
    $form_data_array = @$_POST
    that's not working for some reason. What I have tried is

    PHP Code:
    $array 'item'=>@$_POST['item'
    short variable names are on btw.

  8. #8
    SitePoint Addict melchiorus's Avatar
    Join Date
    Jun 2004
    Location
    Indiana
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by subnet_rx
    that's not working for some reason. What I have tried is

    PHP Code:
    $array 'item'=>@$_POST['item'
    short variable names are on btw.
    Just to clarify here...you just typed that in shorthand right? It should be like this, shouldn't it?

    PHP Code:
    $array = array('item'  =>  $_POST['item']); 
    What exactly is the error? Can you copy and paste it?
    -Melchior (Stephen Craton)

  9. #9
    SitePoint Enthusiast dodga's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't hide errors by using @

    But back ontopic:
    If you don't know whether a field was submitted by the form, use isset() or empty() before you access that field. Always check if the data you want to use actually is there

    (in fact, I think error_reporting(E_ALL) should really be used while developing, but that's another story...)
    remember my name - you'll scream it later

  10. #10
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just make sure $_POST['item'] has value.

    PHP Code:
    $item = (!empty($_POST['item'])) ? $_POST['item'] : null

  11. #11
    SitePoint Wizard subnet_rx's Avatar
    Join Date
    Aug 2001
    Location
    Hattiesburg, MS
    Posts
    1,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    will that work in an array?

  12. #12
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,671
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    subnet,

    Jump uses almost the identical solution I use (except that his does not allow either a value of 0 or an empty value (an empty string)). Give this a go ...

    PHP Code:
    $item = (isset($_POST['item']) && !('' == $_POST['item'])) ? $_POST['item'] : NULL
    This will first test whether the POST variable is set then (ONLY IF IT IS SET) check it against an empty value before assigning the value to $item.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  13. #13
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, a key can point to null in an array. If you don't mind having null values. It's kind of vague what you are really trying to accomplish but that will work. Ideally, everything in an array has a value. Even if you create a placemarker like 'none' or 'N/A'. Then just check if it's != 'none' when outputing values. I'm sure you can also just check if it's != null also.

  14. #14
    SitePoint Addict Chillijam's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Erm.... How about

    PHP Code:
    if($_POST){
      foreach(
    $_POST as $k=>$v){
          
    $myNewArray[$k] = $v;
      }

    or did I misunderstand the original question?
    Your mind is like a parachute. It works best when open.
    (HH The Dalai Lama)

  15. #15
    SitePoint Addict Chillijam's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or maybe...

    PHP Code:
    $keysToGet = array ("username","image","favourite_pie","Monkey","Eyes");

    foreach(
    $keysToGet as $k){
      if(
    $_POST[$key]){
         
    $myNewArray[$key] = $_POST[$key];
      }

    will limit your output array to just those fields you want to see.

    your array $myNewArray would then be something like...
    PHP Code:
    $myNewArray = array (
      
    "username" => "posted_user_name",
      
    "image" => "posted url to image",
      
    "Eyes" => "yes thanks"

    if you add an "else" to the code, you can make sure that all keys are set to something, even if it is "null";
    Your mind is like a parachute. It works best when open.
    (HH The Dalai Lama)

  16. #16
    SitePoint Wizard subnet_rx's Avatar
    Join Date
    Aug 2001
    Location
    Hattiesburg, MS
    Posts
    1,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm trying to do a shopping cart actually and so my next page tries to set every value on the form page to a value through the code I posted above. But, if they don't select an item in a category, then I get an error. I think I'm going to add in a choice to "Select None" so it will be a little more clear to the consumer and I'll be able to pass a null value to the array.


Bookmarks

Posting Permissions

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