JaymeNYC Posted July 3, 2008 Share Posted July 3, 2008 I'm still pretty new to PHP, and been gettin some help on my PHP form but I can't get past this one part, so don't know what to do! I'm trying to create a valid form that has 2 fields that are required, "Name, Email" and 1 field that is not and thats "Comments". Am I doin this the right way? <div id="contactform"><?php if (isset($_SESSION['error'])) { echo $_SESSION['error']; unset($_SESSION['error']); // this will reset the $_SESSION['error'] container } ?> <form method="post" action="thanks.php"> <div class="grp"><label for="name">Your Name</label><br /> <input class="" id="name" type="text" name="name" onfocus="this.style.backgroundColor='#f7ffe3' " onblur="this.style.backgroundColor='#fff'" /></div> <div class="grp"><label for="email">Your Email</label><br /> <input class="" id="email" type="text" name="email" onfocus="this.style.backgroundColor='#f7ffe3' " onblur="this.style.backgroundColor='#fff'" /></div> <div class="grp"><label for="txt">Your Message</label><br /> <textarea id="txt" name="txt" rows="" cols="" onfocus="this.style.backgroundColor='#f7ffe3' " onblur="this.style.backgroundColor='#fff'" ></textarea></div> <div class="grp2"><input class="formsubmit" type="submit" value="Submit" name="submit" /></div> </form> </div> Above is the contact form from the contact.php form page. It said I was missing somethin in the php file so my whole contact page wouldnt load and i saw that the unset didnt have a ")" on the end so it looked like this before I added unset($_SESSION['error']; thanks.php is below <?php session_start(); // this works well with redux - ie. form action = $_SERVER['PHP_SELF'] if(isset($_POST['submit'])) { // set a function that will clean your data function clean($value) { $var = strip_tags($value); $var = trim($value); return $var; } // still clean all data that is on the form $name = clean($_POST['name']); $email = clean($_POST['email']); $txt = clean($_POST['txt']); // check to see if field has been filled in at all // this is the spot to place the *manditory fields* // to validate that data has been entered. // If you don't require it as a manditory field then get rid of it. $_SESSION['error'] = NULL; if (empty($name)) { $_SESSION['error'] .= "You forgot to enter your name!<br />"; $name = FALSE; } if (empty($email)) { $_SESSION['error'] .= "You forgot to enter your email!<br />"; $e = FALSE; } // if all is true then send the email if ($name && $email) { $to = "[email protected]"; $subject = "Company Form Filled"; $body = " Name: $name\n\n Email: $email\n\n Body: $txt"; $sent = mail($to, $subject, $body); if ($sent) { header ("Location: http://www.website.com/thankyou.php"); } // end if $sent } // end if all var are true } // end if post submit // then within your form page where you want // the error message to display, have the following code: ?> When I hit the submit button and I leave a field blank, it goes to a blank page. Is how I did this right so far? Whats the best way to report errors? I would like to use this on my other sites but don't know if I'm coding this the right way to use it over and over again. Thanks Link to comment https://forums.phpfreaks.com/topic/113126-php-form/ Share on other sites More sharing options...
dannyb785 Posted July 3, 2008 Share Posted July 3, 2008 lol the answer was easy(took me 20 minutes to figure out)... basically... it goes blank because you say "if everything is great, then email... " you have no else statement in the case that email or name was blank. p.s. you shoud also make sure the textarea wasn't left blank Link to comment https://forums.phpfreaks.com/topic/113126-php-form/#findComment-581171 Share on other sites More sharing options...
Pickle Posted July 3, 2008 Share Posted July 3, 2008 it seems ok and you would expect to go to a blank page. You are saying the following: 1. if everything is ok send an email - there is no html below this for the browser to show i.e. thanks for contacting us etc. You are not asking it to do anything if one of the fields has been left blank. if ($name && $email) { $to = "[email protected]"; $subject = "Company Form Filled"; $body = " Name: $name\n\n Email: $email\n\n Body: $txt"; $sent = mail($to, $subject, $body); if ($sent) { header ("Location: http://www.website.com/thankyou.php"); } // end if $sent } // end if all var are true ---- there should be an else here to catch the fact that the user may have missed a filed out. ---- } // end if post submit also be careful with your variables if (empty($name)) { $_SESSION['error'] .= "You forgot to enter your name!<br />"; $name = FALSE; ---- maybe you should call this variable something different so you dont get confused between the two see below for email } if (empty($email)) { $_SESSION['error'] .= "You forgot to enter your email!<br />"; $e = FALSE; --- here you have called the variable $e however below you check to see if $email is false } hope this helps a bit Link to comment https://forums.phpfreaks.com/topic/113126-php-form/#findComment-581181 Share on other sites More sharing options...
JaymeNYC Posted July 3, 2008 Author Share Posted July 3, 2008 it seems ok and you would expect to go to a blank page. You are saying the following: 1. if everything is ok send an email - there is no html below this for the browser to show i.e. thanks for contacting us etc. You are not asking it to do anything if one of the fields has been left blank. if ($name && $email) { $to = "[email protected]"; $subject = "Company Form Filled"; $body = " Name: $name\n\n Email: $email\n\n Body: $txt"; $sent = mail($to, $subject, $body); if ($sent) { header ("Location: http://www.website.com/thankyou.php"); } // end if $sent } // end if all var are true ---- there should be an else here to catch the fact that the user may have missed a filed out. ---- } // end if post submit also be careful with your variables if (empty($name)) { $_SESSION['error'] .= "You forgot to enter your name!<br />"; $name = FALSE; ---- maybe you should call this variable something different so you dont get confused between the two see below for email } if (empty($email)) { $_SESSION['error'] .= "You forgot to enter your email!<br />"; $e = FALSE; --- here you have called the variable $e however below you check to see if $email is false } hope this helps a bit A little confused, mostly with the changing of $email to $e. What does that do? I'm very new to php so sorry if I don't pickup on the easy stuff! Link to comment https://forums.phpfreaks.com/topic/113126-php-form/#findComment-581232 Share on other sites More sharing options...
dannyb785 Posted July 4, 2008 Share Posted July 4, 2008 ^ did you realize the problem? It's a pretty simple fix. In fact, it's not even an error. You just didn't put an else statement Link to comment https://forums.phpfreaks.com/topic/113126-php-form/#findComment-581486 Share on other sites More sharing options...
JaymeNYC Posted July 4, 2008 Author Share Posted July 4, 2008 Where do I put it though? I'm REALLY new to php I was gettin help with this but haven't been able to lately so I'm lookin for help here. I did come across this page http://mobygreen.com/contact/#usermessageb which has the fields light up if they were not filled out correctly. Is this a whole different type of form? Also, where can I find some basic->intermediate php tutorials so I can learn faster? I feel handicapped. Link to comment https://forums.phpfreaks.com/topic/113126-php-form/#findComment-581491 Share on other sites More sharing options...
darkfreaks Posted July 4, 2008 Share Posted July 4, 2008 it is really easy put an else if statement to check for empty name and email <?php ini_set('error_reporting',E_ALL); // error reporting ?> Link to comment https://forums.phpfreaks.com/topic/113126-php-form/#findComment-581517 Share on other sites More sharing options...
JaymeNYC Posted July 4, 2008 Author Share Posted July 4, 2008 <?php session_start(); // this works well with redux - ie. form action = $_SERVER['PHP_SELF'] if(isset($_POST['submit'])) { // set a function that will clean your data function clean($value) { $var = strip_tags($value); $var = trim($value); return $var; } // still clean all data that is on the form $name = clean($_POST['name']); $email = clean($_POST['email']); $txt = clean($_POST['txt']); // check to see if field has been filled in at all // this is the spot to place the *manditory fields* // to validate that data has been entered. // If you don't require it as a manditory field then get rid of it. $_SESSION['error'] = NULL; if (empty($name)) { $_SESSION['error'] .= "You forgot to enter your name!<br />"; $name = FALSE; } if (empty($email)) { $_SESSION['error'] .= "You forgot to enter your email!<br />"; $e = FALSE; } // if all is true then send the email if ($name && $email) { $to = "[email protected]"; $subject = "Company Form Filled"; $body = " Name: $name\n\n Email: $email\n\n Body: $txt"; $sent = mail($to, $subject, $body); if ($sent) { header ("Location: http://www.site.com/36/thankyou.php"); } // end if $sent } // end if all var are true if (empty($name)) { $_SESSION['error'] .= "You forgot to enter your name!<br />"; $name = FALSE; } if (empty($email)) { $_SESSION['error'] .= "You forgot to enter your email!<br />"; $e = FALSE; } } // end if post submit else { ini_set('error_reporting',E_ALL); // error reporting } ?> Ahh I'm tired. I have alot more I have to have done by the morning and would just rather have some1 do this lil part so is 15$ enough for 2 PHP cross-browser valid forms? Required field errors reporting to a div ("Name field Required.") email field must have @ (whatever email validation requires) Completed form sends to email Field color change if not completed properly Random answerable question (Captcha?) Thank you message appearing after form is validated/sent Anyone Interested? Just tired of lookin at code Paypal of course :D :D Link to comment https://forums.phpfreaks.com/topic/113126-php-form/#findComment-581529 Share on other sites More sharing options...
darkfreaks Posted July 4, 2008 Share Posted July 4, 2008 message me i will do it Link to comment https://forums.phpfreaks.com/topic/113126-php-form/#findComment-581532 Share on other sites More sharing options...
JaymeNYC Posted July 4, 2008 Author Share Posted July 4, 2008 I'm not able to send private messages, do you have aim? Link to comment https://forums.phpfreaks.com/topic/113126-php-form/#findComment-581542 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.