
|
View Full Version : Need simple help with PHP script to make "all fields required"...
Frosty 11-20-2003, 11:11 PM I've got the below PHP script that i've always used. But even though i clearly state on my site to customers that they must fill out and complete ALL fields they STILL leave several fields blank... some people just don't understand simple instructions and it's getting on my nerves. Some people don't even bother entering their first name for pete's sake.
Someone once showed me what codes i need to add to the below script so that it won't allow the customer to submit the form unless they fill out all the fields. Does anyone know what i need to add to accomplish this? I forgot how...
:confused:
<?
$youremail="bla@mydomain.com" ;
$yoursubject="Need Assistance";
$thankspage="www.bla.com";
mail("$youremail", "$yoursubject", "
First Name: $x_First_Name
Last Name: $x_Last_Name
Email Address: $x_Email
Your Domain Name: $x_Domain
Hosting Plan: $x_Amount
Front Page: $x_Front_Page
Add New Domain: $x_New_Domain
","From: $youremail\nReply-To:$youremail\nContent-Type: text/html; charset=iso-8859-1: PHP/");
header('Location:'.$thankspage);
?>
WLHosting 11-20-2003, 11:23 PM Could you post your <form> </form> so that we can see what you are doing?
monkey junkie 11-20-2003, 11:27 PM What's wrong with a simple if statement?
i.e.
if (($field1=="") || ($field2==""))
echo "You did not fill out all the required fields";
else
echo "Thank you."
Is that what you are looking for??
WLHosting 11-20-2003, 11:30 PM Yes that is what he is looking for. It will get to be a long if statement if he has lots of fields that he has to be required.
hycloud 11-20-2003, 11:31 PM Yeah, it's not that hard. If you know PHP programming, just check to see which require fields are missing with "if" statements.
hiryuu 11-21-2003, 12:54 AM For a quick hack, try this:
if ( array_search('', $_POST) ) {
echo "You must fill in all required fields";
exit;
}
That only works if they use the original form, since it won't pick up on fields that are not included at all. For more security, you can use this:
$required = array('name','address', ...);
foreach ( $required as $field ) {
if ( @empty($_POST[$field]) ) {
echo "You must fill in the $field field";
exit;
}
}
pack tloss 11-21-2003, 01:47 AM Or, what you could do is add a javascript function on the page with the form, so if the input isn't there, it will have a popup that says you need to fill in that field. I tend to do that with forms more. Otherwise I'd recommend what hiryuu is doing.
hycloud 11-21-2003, 03:13 AM I personally would not rely on javascript to do my form checking.
pack tloss 11-21-2003, 03:49 AM Originally posted by hycloud
I personally would not rely on javascript to do my form checking.
We all do it our own way.
**Edit: What's so bad about jscript? :P
banner 11-21-2003, 04:01 AM If you rely on javascript for input checking then you are relying on the user leaving javascript on. In most cases you are best off actually using a combination of client side and server side validation. That way most clients can get the instant results from the javascript without having to wait for the page to post and return and your script is protected from malicious input.
platinum 11-21-2003, 05:54 AM Originally posted by pack tloss
We all do it our own way.
**Edit: What's so bad about jscript? :P
It's okay as an added extra if you feel it needs it, but certainly not something you "rely" on. All valididy checking should be done server side. :)
Frosty 11-21-2003, 11:08 AM Thanks for the suggestions. I just remembered what i added to the script to force you to complete all fields. I pasted it below. But when i use the below i get a weird error. It works fine but aftre you submit the form it shows the correct page but with a weird error under it. I'm going to try one of your suggestions instead, see if that helps. Or does anyone see an error in the below?
<?
$youremail="bla@mydomain.com" ;
$yoursubject="Need Assistance";
$thankspage="www.bla.com";
if (!$x_First_Name || !$x_Last_Name || !$x_Email || !$x_Domain || !$x_Amount|| !$x_Front_Page || !$x_New_Domain) {
echo "<html><body><b><font color=red>You forgot something! Go back and try again!!!</font></b></body></html>";
}
mail("$youremail", "$yoursubject", "
First Name: $x_First_Name
Last Name: $x_Last_Name
Email Address: $x_Email
Your Domain Name: $x_Domain
Hosting Plan: $x_Amount
Front Page: $x_Front_Page
Add New Domain: $x_New_Domain
","From: $youremail\nReply-To:$youremail\nContent-Type: text/html; charset=iso-8859-1: PHP/");
header('Location:'.$thankspage);
?>
Frosty 11-21-2003, 11:14 AM hiryuu,
I'm going to try yours... did i use your codes correctly in the below exmaple? Not sure if i put them in the right place or whatever. Thanks:
<?
$youremail="bla@mydomain.com" ;
$yoursubject="Need Assistance";
$thankspage="www.bla.com";
$required = array('x_First_Name','Last_Name','x_Email',x_Domain' ...);
foreach ( $required as $field ) {
if ( @empty($_POST[$field]) ) {
echo "You must fill in the $field field";
exit;
}
}
mail("$youremail", "$yoursubject", "
First Name: $x_First_Name
Last Name: $x_Last_Name
Email Address: $x_Email
Your Domain Name: $x_Domain
","From: $youremail\nReply-To:$youremail\nContent-Type: text/html; charset=iso-8859-1: PHP/");
header('Location:'.$thankspage);
?>
hiryuu 11-21-2003, 04:11 PM You may want to use the PHP code tags in your posts, so the indenting is shown.
That should probably be
$required = array('x_First_Name','Last_Name','x_Email','x_Domain');
If that's not working, I'll have to go look at my checking code. I may have missed something.
arcodesign 11-21-2003, 04:28 PM Here is the way I do it:
<?php
//form has been sent, check for errors;
if (strlen($sendto) == 0) {
$error = "Please choose a type of subject.<br />";
}
if (strlen($name) < 3) {
$error .= "Please input your full name.<br />";
$name = "";
}
if (eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $email)) {
null; // e-mail is valid;
} else {
$error .= "Please input a valid email address.<br />";
$email = "";
}
if (strlen($body) == 0) {
$error .= "Please input a question or comment.";
$body = "";
}
if (isset($error)) // if there are errors, print this;
{
?>
//html code goes here, or just print the error with a back link
<?php print ($error); ?><br /><br /><a href="javascript: history.go(-1);">Click here to go back.</a>
(html code)
<?php
exit();
} else { //there are no errors, so send user to a different page or print success message;
?>
(html or php code)
<?php
} //end else statement;
?>
Obviously, you will need to hack it up a bit and change form field names and other things. This is just a little snippet of the code from a php contact form I wrote.
There are also many javascript form validators that will check a form before it is even submitted.
Frosty 11-27-2003, 06:40 PM Sorry to bump this thread but I stil haven't gotten this thing to work. I tried the below codes but I get some weird looking parse error when submitting the form. Does anyone here see an error in the below? I just want all the fieds to be required but it doesn't want to work :(
Thanks
<?
$youremail="bla@mydomain.com" ;
$yoursubject="Need Assistance";
$thankspage="www.bla.com";
$required = array('x_First_Name','Last_Name','x_Email',x_Domain' ...);
foreach ( $required as $field ) {
if ( @empty($_POST[$field]) ) {
echo "You must fill in the $field field";
exit;
}
}
mail("$youremail", "$yoursubject", "
First Name: $x_First_Name
Last Name: $x_Last_Name
Email Address: $x_Email
Your Domain Name: $x_Domain
","From: $youremail\nReply-To:$youremail\nContent-Type: text/html; charset=iso-8859-1: PHP/");
header('Location:'.$thankspage);
?>
arcodesign 11-27-2003, 07:03 PM Where are you defining the $field variable?
Frosty 11-27-2003, 07:13 PM Oh whoops, I guess that would be nowhere :)
So where it says:
if ( @empty($_POST[$field]) )
I'm supposed to put:
if (x_First_Name@empty($_POST[$field]) )
Is that correct? I have a feeling i'm waaaaay off :crap:
arcodesign 11-27-2003, 07:17 PM Try this:
if (!isset $_POST[x_First_Name])
and do that for every form field.
If you want, I can send you a php contact script that I wrote which you could easily adapt to your purposes. PM me if you want to.
arcodesign 11-27-2003, 07:25 PM Oops! it should be:
if (!isset $_POST['x_First_Name'])
and then of course you print the error.
|