Page 1 of 1

How to use POST

Posted: Fri Jul 05, 2002 11:06 am
by cruse
I am trying to check if a group of text boxes have entries.
I have tried to use this.
if (empty($_POST));

I want to know if all the variable are empty, but
this is not working.

Thanks,
Cruse

$_POST

Posted: Fri Jul 05, 2002 11:37 am
by BDKR
Hey,

Since $_POST is an array, you'll have to check each element (unless someone knows of another way that I'm not considering). The empty() function doesn't work on arrays as a whole. It will work on elements in an array no problem.

OK, here's some code for ya. I'll bill you later :twisted: .

Code: Select all

# Isn't it actually $_POST_VARS? Correct me if I'm wrong
while(list($key, $val) = each($_POST))
    {  
    if(empty($val); 
    echo "$key is empty<br>"; 
    &#125;
The above should iterate over each element of the array and check to see if it's empty. If it finds an empty element, it will echo that elements name to the screen.

Also, take a look at the each() and list() functions. Also read some tuts on arrays as they are extremely important to programming.

Hope this helps,
BDKR (TRC)

Posted: Fri Jul 05, 2002 11:50 am
by hob_goblin
well, in older versions it was $HTTP_POST_VARS but now its $_POST and the others are being deprecated..

but $_POST contains gibberish most of the time so i'd just try..

Code: Select all

if(empty($_POST&#1111;'key']))&#123;
//statements
&#125;

Posted: Fri Jul 05, 2002 1:26 pm
by BDKR
The only problem with

Code: Select all

if(empty($_POST&#1111;'key']))&#123;
//statements
&#125;
is that he HAS TO KNOW the key of each element he is interested in. But what if the form (or whatever it is) is huge but he only has a small number of required vars in the form? You know, vars that must be filled in while others are optional. If he has 30 vars, writing out 30 if statements is less than efficient.

Instead, you could create an array with the vars that are required then check the $_POST array against it.

An example:

Code: Select all

# Create array
$required_vars = array(user_name, user_pass, user_email, user_aim, user_icq);

# Now check against posted vars
while(list($key, $val) = each($_POST))
    &#123;
    if(empty($val) && in_array($key, $required_vars))
        &#123; $missing_vars&#1111;] = $key; &#125;
     &#125;
Now you can just iterate through $missing_vars to know which variables of the required vars were not posted to the script and take appropriate action.

Also, what is the gibberish you are talking about? Is it scrambled info or some other funkmaster data coming from nowhere? I'm curious to know what this is all about.

Later on,
BDKR (TRC)

Posted: Fri Jul 05, 2002 2:04 pm
by hob_goblin
I can't find the old post, but there was a whole topic about the contents of $_POST..

$_POST always exists.. so if you do

"<?=$_POST;?>" it will always print "Array"

but if you use print_r().. it seems empty but.. infact exists

Posted: Fri Jul 05, 2002 2:26 pm
by BDKR
The amount of work I've actually done with $_POST is very limited. The projec that I'm close to completing now started well before 4.2 :? and is big enough that I'm going to finish completely before going back and toying about with it.

But I do think I'm going to do a little testing on it. I'm really curious now.

Later on,
BDKR (TRC)

Posted: Fri Jul 05, 2002 2:43 pm
by protokol

Code: Select all

function is_empty_array($array) &#123;
   foreach ($array as $key=>$index) &#123;
      if (!empty($key))
         return false;
   &#125;
   return true;
&#125;
Beautiful 8)

Posted: Fri Jul 05, 2002 3:04 pm
by BDKR
Looks good :!:

Posted: Fri Jul 05, 2002 3:06 pm
by cruse
I appreciate the code samples.
I'll give them a try.

Cruse