Forum Moderators: coopster

Message Too Old, No Replies

Form Validation

         

Kysmiley

7:57 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



Slowly I'm getting this figured out. Now I have the error messages going where i want them to but I cant figure out how to combine validation for different fields. The form due to how I now have it set up as error_msg, error_msg1 and error_msg2 will only reject the form if one field is not filled in. I tried to use the pipe to combine them but had no luck. Will i have to set a different complete validation for each field. Or can i use an
array (error_msg = 'error messager here'
error_msg1 = 'error message one here') somthing like that
Can you use multiple words in an array? Everything i see on line and in my books show one word one=two sort of
This is the code I now have
####################
# error checking
if(!$_POST['name']) {
$error_msg .= "<p>Please Fill in the Name field</p>";
}
if(!$_POST['email']) {
$error_msg1 .= "<p>Please fill in the email field</p>";
}
if(!$_POST['suggestion']) {
$error_msg2 .= "<p>Please fill in your comments</p>";
}
# end error checking.

if(!$error_msg ¦¦!$error_msg1 ¦¦$error_msg2)
{

# if everything ok,
# define variables from the posted data
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$suggestion = $_POST ['suggestion'];
$messagetype = $_POST['messagetype'];

header("Location: thankyou.php");
}
#############################
But pipe does not work or do as I want so i am still stuck
pat

bfillmer

8:15 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



This may be something like what you are looking for.


if( extract($_POST) ) {
$errmsg = "";

if(!isset($name) ) {
$errmsg .= "<p>Please Fill in the Name field</p>"
}
if(!isset($email) ) {
$errmsg .= "<p>Please fill in the email field</p>"
}
if(!isset($suggestion) ) {
$errmsg .= "<p>Please fill in your comments</p>"
}

if( $errmsg!= "" ) {
header("Location: thankyou.php");
} else {
echo $errmsg;
}
}

Honestly it would be more effecient to do this type of error checking with javascript instead. I've got a function for that if you are interested.

Kysmiley

8:22 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



Thanks bfilmer that wont work either. i want to echo the error message above the field not filled in. When I used that way before it would echo all three or what ever error messages above all fields so if left out two fields it would put two error messages above each field even the one that was filled in. I really dont want to use Java because not everyone leaves their Java capabilities turned on.
Thanks for the reply though
pat

bfillmer

8:30 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



Just change the $errmsg back to three seperate ones and take out:


} else {
echo $errmsg;

Then echo your error messages the way you were doing.

Kysmiley

3:34 am on Dec 15, 2004 (gmt 0)

10+ Year Member



Thanks again Bfillmer. But would that not just give me what I have already, one field validation error_msg with three different replieds. If someone leaves out field two it would go to the thankyou page because of the if(!$error_msg) part. I may be wrong in my thinking but would I not need to set three if(!$error_msg), one for each field? I guess Im looking for an easier way to do this other than repeating the same code three times with three different names to it. Then if i was to do that how would I set three if's like
##################
if(!$error_msg) && if(!$error_msg1) && if(!$error_msg2)
###########################
then go on to the thank-you page if those feilds were set correct. Hope this makes sence
Pat

bfillmer

3:30 pm on Dec 15, 2004 (gmt 0)

10+ Year Member




global $errmsg;
if( extract($_POST) ) {
if( $name == "" ) {
$errmsg["name"] = "<p>Please Fill in the Name field</p>";
}
if( $email == "" ) {
$errmsg["email"] = "<p>Please fill in the email field</p>";
}
if( $suggestion == "" ) {
$errmsg["sug"] = "<p>Please fill in your comments</p>";
}
// Since you have extracted the variables from the
// $_POST array you don't have to set them here,
// just do whatever it is you wanted with the info,
// emailing it or sticking it in a db.
}
if(!count($errmsg) > 0 ) {
header("Location: thankyou.php");
}

Hopefully this will do what you want. Using an array allows you to only have to run one if statement, you can add as many input box names as keys to the array as you want.

Oh yeah, whenever you want to display the error message later on the page just put in this (example uses the name field):


if(isset($errmsg["name"])) { echo $errmsg["name"]; }

Kysmiley

11:11 pm on Dec 15, 2004 (gmt 0)

10+ Year Member



Thanks bfillmer, I will try that and see if it will post the different error messages above the appropriate field that is not filled in. I had somthing like mthat in the beginning but it displayed the error message on a seperate page, so I decided i wanted it to look more professional and follow the look of the site and also post the error messages back on the form above the field not filed in.
Thats why i was trying three different error message names and if was doing almost what i wanted, however i could not get it to validate the three fields. If the one named error_msg was filled in it would process even if the other two were blank. Yet if I left the main one blank and filled in one of the other two i got the correct error messages back and the form would not proccess till the main field was filled in. I hope this makes sence to you. Im still really new at this and am learning how to type things in a way that others will understand what im trying to express. Thanks for your time on this.
Pat

Kysmiley

3:59 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



Ok lets try again, someday i will figure this out by asking questions and playing with it. I tried the code you suggested bfillmer and i am getting a parse error of unexpected $ on line 110 the only thing even close to that line is my include file for my footer and it works find on the original form i am trying to modify. Any ideas/ If you need to see more code let me know Im trying to to post too much here in the form as per TOC
Pat

bfillmer

8:34 pm on Dec 17, 2004 (gmt 0)

10+ Year Member



If you'd like me to take a look at the code go ahead and send me the file: bfillmer at email.com

I'll take a look at it and see if I can't help you out.

Kysmiley

10:11 pm on Dec 17, 2004 (gmt 0)

10+ Year Member



Thanks Bfillmer for all the help. I have finally managed to get this up and running with all the error messages going to the places they need to. I even setup a session so i can carry the name over to the thank-you page, along with mailing me the results. Next will be learning how to setup and transfer some of the information to a database. I will still tweak it some to try to learn how to clean up my code. If you still want to look at it to offer suggestions and tips of writing cleaner code I sure could use the advice. let me know I will send it to u
Pat

bfillmer

3:01 pm on Dec 18, 2004 (gmt 0)

10+ Year Member



Anytime, keep my email address and if you ever need anything, specially during the weekdays, shoot me an email.