How to use POST

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cruse
Forum Newbie
Posts: 9
Joined: Fri Jul 05, 2002 11:06 am
Location: VA

How to use POST

Post 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
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

$_POST

Post 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)
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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;
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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)
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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)
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post 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)
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Looks good :!:
cruse
Forum Newbie
Posts: 9
Joined: Fri Jul 05, 2002 11:06 am
Location: VA

Post by cruse »

I appreciate the code samples.
I'll give them a try.

Cruse
Post Reply