seasexsun Posted December 10, 2007 Share Posted December 10, 2007 I'm trying to make a field mandatory and it's still letting you fill out the form without filling in the email address, which is the field I want to be mandatory. I would really appreciate anyone's input on what is wrong with my script. That would be great. It's a very short script. Thanks <?php if (isset($_POST['Submit']) && isset($_POST['email']) && trim($_POST['email']) != '') { echo '<h1>Thank you for filling out this form!</h1>'; } else { $to = "email@mydomain.com"; $subject = "Website Email"; $email = $_REQUEST['email'] ; $message = $_REQUEST['message'] ; $headers = "From: $email"; $sent = mail($to, $subject, $message, $headers) ; if($sent) {print ""; } else {print "We encountered an error sending your mail"; } ?> <?php } ?> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 10, 2007 Share Posted December 10, 2007 Try <?php if (isset($_POST['Submit'])){ if (isset($_POST['email']) && $_POST['email'] != "") echo '<h1>Thank you for filling out this form!</h1>'; else echo "Please fill the email field out"; } else { $to = "email@mydomain.com"; $subject = "Website Email"; $email = $_REQUEST['email'] ; $message = $_REQUEST['message'] ; $headers = "From: $email"; $sent = mail($to, $subject, $message, $headers) ; if ($sent) { print ""; } else { print "We encountered an error sending your mail"; } } ?> [code] [/code] Quote Link to comment Share on other sites More sharing options...
seasexsun Posted December 10, 2007 Author Share Posted December 10, 2007 Hi pocobueno: That didn't work. I see the change you made was to take out the extra php at the bottom. I had tried that originally. It still doesn't work. It still doesn't make that field mandatory. Do you have any other suggestions? Thanks! Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 10, 2007 Share Posted December 10, 2007 I did more than take out the unnecessary PHP tags. Can you post you form? Quote Link to comment Share on other sites More sharing options...
seasexsun Posted December 10, 2007 Author Share Posted December 10, 2007 Sure... Here's the form with the script how I originally had it. And thanks for your input... <html> <head> </head> <body bgcolor="#FFCC00"> <div> <font face="Arial" size="2"> <table border="0" width="100%" id="table1" bgcolor="#FFFFFF"> <tr> <td width="126"> <table border="0" width="107%" id="table2"> </tr> </table> </td> <td width="797" valign="top"> <td> </td> <td width="434"> </td> </tr> </table> <font face="Arial" size="2"> <table border="0" width="67%" id="table4"> <tr> <td> </td> <td width="434"><font face="Arial" size="2"> Please place your email address here: <input name="email" type="text"> <br> <br> Please write your message to here, and we <br> will contact you shortly:<br> <textarea name="message" rows="2" cols="40"></textarea><br> <input type="submit" value="Click here to send your email!"> <br><br>(If the text has cleared from the form fields, <br>your email has been sent successfully. <br>Please do not reclick.) </font> <p align="left"><font face="Arial" size="2"> </td> </tr> </table> </font> <p align="center"></p> </form> <p align="center"> </p> <p></td> </tr> </table> </font></div> <?php if (isset($_POST['Submit']) && isset($_POST['email']) && trim($_POST['email']) != '') { echo '<h1>Thank you for filling out this form!</h1>'; } else { $to = "email@mydomain.com"; $subject = "Website Email"; $email = $_REQUEST['email'] ; $message = $_REQUEST['message'] ; $headers = "From: $email"; $sent = mail($to, $subject, $message, $headers) ; if($sent) {print ""; } else {print "We encountered an error sending your mail"; } ?> <?php } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 10, 2007 Share Posted December 10, 2007 You had quite a bit of missing stuff. You didn't have an opening form tag, and your submit button didn't have a name. Also, your PHP logic was a bit messed up. So try this and see if it is what you want <html> <head> </head> <body bgcolor="#FFCC00"> <div> <font face="Arial" size="2"> <table border="0" width="100%" id="table1" bgcolor="#FFFFFF"> <tr> <td width="126"> <table border="0" width="107%" id="table2"> </tr> </table> </td> <td width="797" valign="top"> <td> </td> <td width="434"> </td> </tr> </table> <font face="Arial" size="2"> <form method="post"> <table border="0" width="67%" id="table4"> <tr> <td> </td> <td width="434"><font face="Arial" size="2"> Please place your email address here: <input name="email" type="text"> <br> <br> Please write your message to here, and we <br> will contact you shortly:<br> <textarea name="message" rows="2" cols="40"></textarea><br> <input type="submit" name="Submit" value="Click here to send your email!"> <br><br>(If the text has cleared from the form fields, <br>your email has been sent successfully. <br>Please do not reclick.) </font> <p align="left"><font face="Arial" size="2"> </td> </tr> </table> </font> <p align="center"></p> </form> <p align="center"> </p> <p></td> </tr> </table> </font></div> <?php if (isset($_POST['Submit'])){ if (isset($_POST['email']) && $_POST['email'] != ""){ $to = "email@mydomain.com"; $subject = "Website Email"; $email = $_REQUEST['email'] ; $message = $_REQUEST['message'] ; $headers = "From: $email"; $sent = mail($to, $subject, $message, $headers) ; if ($sent) { print ""; } else { print "We encountered an error sending your mail"; } } else { echo "You didn't fill in your email!"; } } ?> </body> </html> Also note that any messages will appear at the BOTTOM of your script since your PHP code is at the bottom. I wasn't sure if you wanted it like that, so I left it. Quote Link to comment Share on other sites More sharing options...
seasexsun Posted December 10, 2007 Author Share Posted December 10, 2007 Thank you so so much! It worked! I'll have to go over it and see what I left out and how I could have done wrong, and again. Thanks so much. 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.