WestCoast?s Posted January 7, 2011 Share Posted January 7, 2011 I've been trying to make my contact form engine mandatory for the name, phone number and email fields, but when I upload it on the ftp and test it, I keep getting errors. Here's the code I have that is working without the fields being mandatory: <?php $EmailFrom = "example@gmail.com"; $EmailTo = "example@gmail.com"; $EmailTo = "example@gmail.com"; $EmailTo = "example@gmail.com"; $Subject = "New Lead!!!"; $Name = Trim(stripslashes($_POST['Name'])); $Phone_Number = Trim(stripslashes($_POST['Phone_Number'])); $Email = Trim(stripslashes($_POST['Email'])); $Message = Trim(stripslashes($_POST['Message'])); // validation $validationOK=true; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit; } // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Phone Number: "; $Body .= $Phone_Number; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Message: "; $Body .= $Message; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); // redirect to success page if ($success) {//&& ($Phone_Number != "" || $Email != "") { print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">"; } else{ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; } ?> I would like it to return something that says: "Enter Your Name/Phone Number/Email" corresponding to the appropriate field they didn't fill out. Thanks for any input you can provide! Quote Link to comment Share on other sites More sharing options...
rondog Posted January 7, 2011 Share Posted January 7, 2011 I dont know what errors you are getting, but I am sure one of them is a 'Trim' error. It is 'trim', note the caps. Quote Link to comment Share on other sites More sharing options...
denno020 Posted January 7, 2011 Share Posted January 7, 2011 Are you able to use javascript?? function validate_form ( ) { valid = true; if ( document.form.field1.value == "" ) { alert ( "Please enter the field1." ); valid = false; } else if ( document.form.field2.value == "" ) { alert ( "Please enter info for the field2." ); valid = false; } else if ( document.form.field3.value == "" ) { alert ( "Please enter some info into field 3" ); valid = false; } return valid; } <form onsubmit="return validate_form ( );"> That will pop up a message that will check each field (1, 2 and 3), if there is anything in there. It will pop up a message if there is nothing inside when submit is pressed. Change field1 etc above to the id of each field you want to check. You can also do this in PHP, but it requires a page refresh, obviously. So javascript would be the quicker option, but here is how you would do it using php (pseudo code, cuz I can't find where I've already written it): //Initialize 3 variables (or however many you need - one for each field) - make them equal to = ""; //Check if form has been submitted or not //Multiple If statements checking $_POST for each field and if it's an empty string //Inside each If, you will set the 3 variables above to have a message, saying, please fill in this field (or something more specific) //If all 3 variables are filled in correctly, then you either include your form handling script, or re-direct to it. //Your form will have no action (this will just reload this same page when submit is pressed) //Echo the above 3 variables next to the fields that they correspond to (at first these will be blank, as no data has been POSTED so the above if statements shouldn't run) That's a general idea of what you could do, which will hopefully get you started on the right track Denno Quote Link to comment Share on other sites More sharing options...
Zurev Posted January 7, 2011 Share Posted January 7, 2011 I dont know what errors you are getting, but I am sure one of them is a 'Trim' error. It is 'trim', note the caps. As to my surprise as well, Trim() will work just the same as trim(). Westcoast, please give us the exact errors you're getting, and enclose your code in code or php tags. Are you able to use javascript?? Javascript isn't validation, it may work for this scenario, but to the OP, javascript can be disabled and is run client side. If you're going to do this you might as well validate server side as well. Javascript is nice because it can be done without reloading the page, causing no strain on the server. Quote Link to comment Share on other sites More sharing options...
rondog Posted January 7, 2011 Share Posted January 7, 2011 As to my surprise as well, Trim() will work just the same as trim(). Interesting, I would have not guessed that. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 7, 2011 Share Posted January 7, 2011 Functions are not case-sensitive, variables are. Quote Link to comment Share on other sites More sharing options...
WestCoast?s Posted January 7, 2011 Author Share Posted January 7, 2011 Are you able to use javascript?? function validate_form ( ) { valid = true; if ( document.form.field1.value == "" ) { alert ( "Please enter the field1." ); valid = false; } else if ( document.form.field2.value == "" ) { alert ( "Please enter info for the field2." ); valid = false; } else if ( document.form.field3.value == "" ) { alert ( "Please enter some info into field 3" ); valid = false; } return valid; } <form onsubmit="return validate_form ( );"> That will pop up a message that will check each field (1, 2 and 3), if there is anything in there. It will pop up a message if there is nothing inside when submit is pressed. Change field1 etc above to the id of each field you want to check. You can also do this in PHP, but it requires a page refresh, obviously. So javascript would be the quicker option, but here is how you would do it using php (pseudo code, cuz I can't find where I've already written it): //Initialize 3 variables (or however many you need - one for each field) - make them equal to = ""; //Check if form has been submitted or not //Multiple If statements checking $_POST for each field and if it's an empty string //Inside each If, you will set the 3 variables above to have a message, saying, please fill in this field (or something more specific) //If all 3 variables are filled in correctly, then you either include your form handling script, or re-direct to it. //Your form will have no action (this will just reload this same page when submit is pressed) //Echo the above 3 variables next to the fields that they correspond to (at first these will be blank, as no data has been POSTED so the above if statements shouldn't run) That's a general idea of what you could do, which will hopefully get you started on the right track Denno Denno, I'd been trying something similar to what you suggested. I'll give it a go again. The code you supplied is a bit different, so maybe it'll work out the kinks. Quote Link to comment Share on other sites More sharing options...
denno020 Posted January 7, 2011 Share Posted January 7, 2011 I assume you're talking about the javascript code? Because I didn't actually give any php code lol. It should work perfectly. Very simple, and effective. Denno Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 7, 2011 Share Posted January 7, 2011 Javascript is not validation. Validation must be done server-side. Quote Link to comment Share on other sites More sharing options...
WestCoast?s Posted January 7, 2011 Author Share Posted January 7, 2011 So here's the new code I input: <?php $EmailFrom = "example@gmail.com"; $EmailTo = "example@gmail.com"; $EmailTo = "example@gmail.com"; $EmailTo = "example@gmail.com"; $Subject = "New Lead!!!"; $Name = check_imput($_POST['Name'],"Enter Your Name"); $Phone_Number = check_imput($_POST['Phone_Number'],"Enter Your Phone Number"); $Email = check_imput($_POST['Email'],"Enter Your Email"); $Message = check_imput($_POST['Message']); <?php function check_input($data, $problem='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { die($problem); } return $data; } ?> // validation $validationOK=true; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm/">"; exit; } // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Phone Number: "; $Body .= $Phone_Number; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Message: "; $Body .= $Message; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); // redirect to success page if ($success) {//&& ($Phone_Number != "" || $Email != "") { print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php/">"; } else{ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm/">"; } ?> and from there I get a: Parse error: syntax error, unexpected '<' on line 13. Remove that and get: Parse error: syntax error, unexpected '?' on line 13. Remove that and get: Parse error: syntax error, unexpected T_FUNCTION on line 14 I'm gonna try the original with Denno's java Quote Link to comment Share on other sites More sharing options...
Abuda Posted January 7, 2011 Share Posted January 7, 2011 $EmailTo = "example@gmail.com"; $EmailTo = "example@gmail.com"; $EmailTo = "example@gmail.com"; First two lines here are useless. // validation $validationOK=true; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit; } (!$validationOK) will never return TRUE since you already set validationOK to TRUE one line earlier. Am I imagining this? Quote Link to comment Share on other sites More sharing options...
WestCoast?s Posted January 7, 2011 Author Share Posted January 7, 2011 $EmailTo = "example@gmail.com"; $EmailTo = "example@gmail.com"; $EmailTo = "example@gmail.com"; First two lines here are useless. // validation $validationOK=true; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit; } (!$validationOK) will never return TRUE since you already set validationOK to TRUE one line earlier. Am I imagining this? Abuda, I have those three since I have it sending to three separate email addresses. The original code works, I just want to make the Name, Phone, and Email mandatory. Denno, I tried what you suggested. Looks like this: <?php $EmailFrom = "Lead@CashCowComplexes.com"; $EmailTo = "cash@cashcowcomplexes.com"; $EmailTo = "Gary@CashCowComplexes.com"; $EmailTo = "GaryLoanDocs@gmail.com"; $Subject = "New Lead!!!"; $Name = Trim(stripslashes($_POST['Name'])); $Phone_Number = Trim(stripslashes($_POST['Phone_Number'])); $Email = Trim(stripslashes($_POST['Email'])); $Message = Trim(stripslashes($_POST['Message'])); function validate_form ( ) { valid = true; if ( 'Name" == "" ) { alert ( "Please Enter Your Name." ); valid = false; } else if ( 'Phone_Number' == "" ) { alert ( "Please Enter Your Phone Number." ); valid = false; } else if ( 'Email" == "" ) { alert ( "Please Enter Your Email" ); valid = false; } return valid; } // validation $validationOK=true; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit; } // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Phone Number: "; $Body .= $Phone_Number; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Message: "; $Body .= $Message; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); // redirect to success page if ($success) {//&& ($Phone_Number != "" || $Email != "") { print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">"; } else{ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; } ?> I'm getting an error message on line 14 for an unexpected "=". I'm pretty new to PHP and my web guy fell off the face of the earth, so I've been trying to tackle this myself. Any suggestions on where to tweak? Do I need the validate form and the validate, or is that redundant? Quote Link to comment Share on other sites More sharing options...
denno020 Posted January 7, 2011 Share Posted January 7, 2011 surround the javascript with: <script type="text/javascript"> //function goes here </script Need to tell the browser to read the code as javscript, at the moment it's reading it as php. You will also need to echo it out. Or just escape from php (?>) just before the <script type="text/javascript"> declaration, and then back into php (<?php) afterwards. Denno Quote Link to comment Share on other sites More sharing options...
revraz Posted January 7, 2011 Share Posted January 7, 2011 In regards to your 3 Email Variables, since they are the exact same name, all you are doing is overwriting the previous one with the next one, you need 3 different email variables or an array. Second, you are missing your $ variable character here for Name, Phone_Number and Email. Also, you are using a double quote instead of a single quote after Name. function validate_form ( ) { valid = true; if ( 'Name" == "" ) { alert ( "Please Enter Your Name." ); valid = false; } else if ( 'Phone_Number' == "" ) { alert ( "Please Enter Your Phone Number." ); valid = false; } else if ( 'Email" == "" ) { alert ( "Please Enter Your Email" ); valid = false; } return valid; } All your errors are Typos, and if you used a editor with syntax highlighting or even used this forum's PHP code tags, you would see your errors. Quote Link to comment Share on other sites More sharing options...
Abuda Posted January 7, 2011 Share Posted January 7, 2011 Tested. form.php <?php if($_POST['submit']) { // only checking for empty fields, regexp can do a lot more. $error = ""; if( $_POST['name'] == "") { $error .= "<br />- Name field is empty."; } if( $_POST['phone'] == "") { $error .= "<br />- Phone field is empty."; } if( $_POST['email'] == "") { $error .= "<br />- Email field is empty."; } if( $_POST['message'] == "") { $error .= "<br />- Message field is empty."; } $EmailFrom = "example@gmail.com"; $EmailTo = "example@gmail.com"; $Subject = "New Lead!!!"; $name = Trim(stripslashes($_POST['name'])); $phone = Trim(stripslashes($_POST['phone'])); $email = Trim(stripslashes($_POST['email'])); $message = Trim(stripslashes($_POST['message'])); if($error == "") { // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $name; $Body .= "\n"; $Body .= "Phone Number: "; $Body .= $phone; $Body .= "\n"; $Body .= "Email: "; $Body .= $email; $Body .= "\n"; $Body .= "Message: "; $Body .= $message; $Body .= "\n"; // send email mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); header('Location: contactthanks.php'); }else{ echo<<<END <html> <head></head> <body> <form action="form.php" method="POST"> Name: <input type="text" name="name" value="$name"><br /> Phone Number: <input type="text" name="phone" value="$phone"><br /> Email: <input type="text" name="email" value="$email"><br /> Message: <input type="text" name="message" value="$message"><br /> <input type="submit" name="submit"> <p>$error</p> </form> </body> </html> END; } }else{ echo<<<END <html> <head></head> <body> <form action="form.php" method="POST"> Name: <input type="text" name="name" value=""><br /> Phone Number: <input type="text" name="phone" value=""><br /> Email: <input type="text" name="email" value=""><br /> Message: <input type="text" name="message" value=""><br /> <input type="submit" name="submit"> </form> </body> </html> END; } ?> As many have stated, never use Javascript for form validation (Unless you're validating again via a server-side language). Quote Link to comment Share on other sites More sharing options...
WestCoast?s Posted January 7, 2011 Author Share Posted January 7, 2011 Denno, Thanks for the clarification. revraz, Thanks for the heads up on the typos. Even after fixing those it still didn't make the fields mandatory. It would allow me to submit the form and take me through to the correct follow up page, but I can still submit it blank. Abuda, I also tried yours and it didn't quite work either. I'd hit submit and it would take me to another page solely dedicated to the contact form, then when I filled that out it would pop up as a hosting error with the page not found. I combined what you suggested with the original to get this: <?php if($_POST['submit']) { // only checking for empty fields, regexp can do a lot more. $error = ""; if( $_POST['name'] == "") { $error .= "<br />- Name field is empty."; } if( $_POST['phone'] == "") { $error .= "<br />- Phone field is empty."; } if( $_POST['email'] == "") { $error .= "<br />- Email field is empty."; } $EmailFrom = "example@gmail.com"; $EmailTo = "example@gmail.com"; $Subject = "New Lead!!!"; $name = Trim(stripslashes($_POST['name'])); $phone = Trim(stripslashes($_POST['phone'])); $email = Trim(stripslashes($_POST['email'])); $message = Trim(stripslashes($_POST['message'])); // validation $validationOK=true; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit; } // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Phone Number: "; $Body .= $Phone_Number; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Message: "; $Body .= $Message; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); // redirect to success page if ($success) {//&& ($Phone_Number != "" || $Email != "") { print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">"; } else{ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; } ?> and it tells me Parse error: syntax error, unexpected $end on the last line of it. MOD Edit: . . . tags added. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 7, 2011 Share Posted January 7, 2011 When posting code, please enclose it in the forum's . . . BCode tags. Quote Link to comment Share on other sites More sharing options...
Abuda Posted January 7, 2011 Share Posted January 7, 2011 Copy the code as provided in my last post and paste it into a new form.php. You only have to replace: mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); header('Location: contactthanks.php'); with: echo "OK"; Since you're on a localhost. And you'll see that it works just fine. You'll then need to modify that to appear the way you need, knowing that you can't simply mix codes together, this is what you're doing and that's why you're getting the syntax error. You're missing a curly bracket at the end of your last code, and I don't think this is the only problem, you'll need to be more careful. Quote Link to comment Share on other sites More sharing options...
WestCoast?s Posted January 8, 2011 Author Share Posted January 8, 2011 Here's what I've got now, and it's still getting a syntax error on the last line... <?php if($_POST['submit']) { // only checking for empty fields, regexp can do a lot more. $error = ""; if( $_POST['name'] == "") { $error .= "<br />- Name field is empty."; } if( $_POST['phone'] == "") { $error .= "<br />- Phone field is empty."; } if( $_POST['email'] == "") { $error .= "<br />- Email field is empty."; } $EmailFrom = "Example@gmail.com"; $EmailTo = "Example@gmail.com"; $Subject = "New Lead!!!"; $name = Trim(stripslashes($_POST['name'])); $phone = Trim(stripslashes($_POST['phone'])); $email = Trim(stripslashes($_POST['email'])); $message = Trim(stripslashes($_POST['message'])); if($error == "") { // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $name; $Body .= "\n"; $Body .= "Phone Number: "; $Body .= $phone; $Body .= "\n"; $Body .= "Email: "; $Body .= $email; $Body .= "\n"; $Body .= "Message: "; $Body .= $message; $Body .= "\n"; // send email echo "OK"; }else{ echo<<<END <html> <head></head> <body> <form action="form.php" method="POST"> Name: <input type="text" name="name" value="$name"><br /> Phone Number: <input type="text" name="phone" value="$phone"><br /> Email: <input type="text" name="email" value="$email"><br /> Message: <input type="text" name="message" value="$message"><br /> <input type="submit" name="submit"> <p>$error</p> </form> </body> </html> END; } ?> Any idea where it's going awry? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 8, 2011 Share Posted January 8, 2011 Count your opening and closing curly braces. There should be the same number of each. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.