Click to See Complete Forum and Search --> : Php Problem


sas
08-06-2003, 01:43 PM
I'm trying to check that all forms have been filled in on a feedback form if they have then it sends a mail and displays a 'well done' page, if they haven't it displays an error page. Thing is all it does is mail me and displays well done whatever i do. I realise it would be a better way to check on a per form basis and echo back to the user which particular box had the problem, but hey i wanted to use an array for the first time.

I think i've used the array wrong, its the first time i've actually tried to do something using an array. Either that or empty() is the wrong function. Anyway does anyone know where i'm going wrong?

<?php
//create short var names
$name = $_POST['name'];
$email = $_POST['email'];
$feedback = $_POST['feedback'];
$toaddress = 'dean@localhost';
$subject = 'Feedback from website';
$mailcontent = 'Customer Name: '.$name."\n"
.'Customer email: '.$email."\n"
."Customer comments: \n".$feedback. "\n";
$fromaddress = 'From: webserver@example.com';
?>
<html>
<head>
<title>Bob's Auto Parts - Feedback</title>
</head>
<body>
<?php
//check form is filled correctly
$empty = array('$name', '$email', '$feedback' );
if(empty($empty))
{
echo '<h1>Feedback not sent</h1>';
echo '<p>You did not fill in all the required forms.</p>';
}
else
{
mail($toaddress, $subject, $mailcontent, $fromaddress);
echo '<h1>Feedback submitted</h1>';
echo '<p>Your feedback has been sent.</p>';
}
?>
</body>
</html>

ph34r
08-06-2003, 01:59 PM
I recently wrote a similar script, but I wanted it very generic to work with all of our existing forms. In the old script, we had a hidden input called "required" that had a comma separated list of all required fields in it.

<input type=hidden required="name,email,feedback">

Here's the code I use to check it:

import_request_variables("gP", "rvar_");

if(isset($rvar_required)){
$req_fields=explode(",",$rvar_required);
// 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;
}
}
for($i=0;$i<sizeof($req_fields);$i++){
if(!isset($_POST[$req_fields[$i]])){
$ok[required]=false;
$bad_field=$req_fields[$i];
break;
}
}
}


Later before sending I check to see if $ok[required] is false, and if so, go to a "display error message" type of function.

sas
08-06-2003, 05:12 PM
thanks for the help. I don't want to seem unthankful, but i'm only doing this to learn myself php. I want to know what exactly is wrong with my code so i can redo it, still keeping it simple so i can completely understand it. Do i need to write something as technical as what you've done just for my small thing? I assumed my code was ok apart from this line:
$empty = array('$name', '$email', '$feedback' );

I assume i'm doing something wrong in trying to create an array like that. My book i'm reading doesn't go into great detail about how to do something like this, it just suggests its a good idea to check the forms have been filled in. Indeed its the first thing i've wrote that wasn't practically a word for word copy of something in the book.

Stuka
08-06-2003, 05:27 PM
Not that this will solve your problem, but this: $name = $_POST['name'];

$email = $_POST['email'];

$feedback = $_POST['feedback'];
can be replaced with this:extract($_POST);

sas
08-06-2003, 06:19 PM
and that lets me access all my variables like i did before? sweet.

Thanks

iDxMan
08-06-2003, 07:17 PM
correction

<input type=hidden required="name,email,feedback">
should be
<input type="hidden" name="required" value="name,email,feedback">


try:

$empty = array($name, $email, $feedback);
//remove the single quotes

foreach ($empty as $foo) {
if (empty($foo)) {
//blank var - error
}
}



-r

sas
08-07-2003, 09:07 AM
thanks, thats sorted the problem, thing is i now get three of everything :D but i'm sure i can sort that now.

Thanks all