Click to See Complete Forum and Search --> : Checking formfields with PHP.


arioch
02-01-2005, 09:45 AM
Hi!

I'm trying to get php to check if all formfields of a "Tell-a-friend" script have been filled out, so i can reward the obedient minion!

How do I do this?

I'm picking up the form to a seperate scriptfile with:

foreach ($_REQUEST as $email)

I'm completely new to programming, so this is propably trivial. All I want is for PHP to check if all fields were filled, and then react on it if the condition is met. It's propably some kind of if/else arrangement.

Bye.

ph34r
02-01-2005, 09:57 AM
I wrote a generic form2mail php script . If there is a field named "required" (hidden input type, values separated by commas *with no spaces*), my script runs thru the following code to flag missing "required" stuff:


// check for required variables
if(isset($rvar_required)){
$req_fields=explode(",",$rvar_required);

// trim any excess fat from the cow
foreach($req_fields as $tmp) {
$tmp = trim($tmp);
}

// text fields names will be in _POST, but have no value
foreach($_POST as $idx => $val){
if(in_array($idx,$req_fields)&&($val=="")){
$ok['required']=false;
$bad_field=$idx;
break;
}
}
// other fields will just not be there at all
for($i=0;$i<sizeof($req_fields);$i++){
if(!isset($_POST[$req_fields[$i]])){
$ok['required']=false;
$bad_field=$req_fields[$i];
break;
}
}
}

arioch
02-01-2005, 10:16 AM
Ok...

It looks like the if/else structure I imagine I'm needing, needs to be defined before the loop itself is run?

I'm aware of the "isset" function, but can it be used to check an entire form in one go, instead of just field by field?

so far I've done this:

<?php
foreach ($_REQUEST as $email) {
mail ($email, $subject, $text);
}
if (isset($_REQUEST['email'])) {
echo $reward; exit;
}
else {
echo $thanks; exit;
}
?>

I'm trying to teach myself programming, and I have discovered that the best way to learn to program for me, is to try to do as much as possible, with as little code as possible. It also really get's you "inside" a language.

arioch
02-01-2005, 02:48 PM
I have now tried with this instead, but it just displays $thanx instead of $reward no matter what, even though all formfileds are filled.:



<?php
if ($email !== NULL) {
echo $reward;
}
else {
echo $thanks;
}
foreach ($_REQUEST as $email) {
mail ($email, $subject, $mailtext);
}
?>


There must be a really simple way to do this I just can't see it...