How to use POST
Moderator: General Moderators
How to use POST
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
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
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
.
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)
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
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>";
}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)
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
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..
but $_POST contains gibberish most of the time so i'd just try..
Code: Select all
if(empty($_POSTї'key'])){
//statements
}The only problem with
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:
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)
Code: Select all
if(empty($_POSTї'key'])){
//statements
}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))
{
if(empty($val) && in_array($key, $required_vars))
{ $missing_varsї] = $key; }
}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)
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
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)
But I do think I'm going to do a little testing on it. I'm really curious now.
Later on,
BDKR (TRC)
- protokol
- Forum Contributor
- Posts: 353
- Joined: Fri Jun 21, 2002 7:00 pm
- Location: Cleveland, OH
- Contact:
Code: Select all
function is_empty_array($array) {
foreach ($array as $key=>$index) {
if (!empty($key))
return false;
}
return true;
}