doctortim Posted January 26, 2009 Share Posted January 26, 2009 Hi guys, Have an important website ready to go live tomorrow night, so any help would be greatly appreciated, anyway... Problem with the contact form, I'm testing it locally (the php and form are on the one page 'contact.php' and the form action posts to itself). Mail is being sent (I expect tha since I'm testing locally), but when I leave the name or email field blank, I dont even trigger my errors. Here is my code: The PHP: <?php // Address error handing. ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); // Check if the form has been submitted. if ( isset ($_POST['submit'])) { $problem = FALSE; // No problems so far. // Check for each value. if (empty ($_POST['name'])) { $problem = TRUE; print '<div id="email_warnings">Please enter your name!</div>'; } if (empty ($_POST['email'])) { $problem = TRUE; print '<div id="email_warnings">Please enter your email address!</div>'; } if (!$problem) { // If there weren't any problems... print '<div id="email_warnings">Your message has been sent<br />Thank You!</div>'; // Send the email. $email_to = 'contact@coreknowledge.com.au'; $subject = $_POST['enquiry_type']; $body = "A contact form email has been received !!/n/n". "The message is from ".$_POST['name'].", with email: ".$_POST['email']."/n/n". "Message Sent: "."/n/n". $_POST['message_body']; mail($email_to, $subject, $body); } else { // Forgot a field. print '<div id="email_warnings">Please try again!</p></div>'; } } ?> Then I have the contact form right after: <form action="contact.php" method="post"> <table class="table_margin" width="343" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="129" align="right" valign="top"><em>Name</em></td> <td width="180" class="cell_margin"><label> <input name="name" type="text" id="textfield" value="first and last name" size="24" /> </label></td> </tr> <tr> <td align="right" valign="top"> </td> <td class="cell_margin"> </td> </tr> <tr> <td align="right" valign="top"><em>Email</em></td> <td class="cell_margin"><input name="email" type="text" id="textfield2" value="example@domain.com" size="24" /></td> </tr> <tr> <td align="right" valign="top"> </td> <td class="cell_margin"> </td> </tr> <tr> <td align="right" valign="top"><em>Enquiry</em></td> <td class="cell_margin"><label> <select name="enquiry_type" id="select"> <option value="sale">Sales & Order Enquiry</option> <option value="feedback">General Feedback</option> <option value="issue">Website Issue</option> </select> </label></td> </tr> <tr> <td align="right" valign="top"> </td> <td class="cell_margin"> </td> </tr> <tr> <td align="right" valign="top"><em>Message</em></td> <td class="cell_margin"><label> <textarea name="message_body" id="textarea" cols="45" rows="5">Type your message here ...</textarea> </label></td> </tr> <tr> <td align="right" valign="top"> </td> <td class="cell_margin"> </td> </tr> <tr> <td align="right" valign="top"> </td> <td class="cell_margin"><label> <input type="submit" name="button" id="button" value="Submit" /> </label></td> </tr> </table> </form> Thanks guys! Quote Link to comment Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 It is because you have a default value in for the email and the name, remove that default value or check if the name is empty or is equal to that default value. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 26, 2009 Share Posted January 26, 2009 It's not the default values. Remove them and it still does not trigger the errors. Time to smack yourself in the forehead. Don't worry, we all do it. Your submit button has a name of "button" <input type="submit" name="button" id="button" value="Submit" /> But, you are testing for a POST item with the name "submit" if ( isset ($_POST['submit'])) { So your page never checks the form data. Either change the name of the submit button or the variable you are testing for. You don't have to check for a particular post value either - just check that $_POST was set if ( isset ($_POST)) { I would also look at revising your error handling to re-populat the fields with any values the user did enter. Quote Link to comment Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 It's not the default values. Remove them and it still does not trigger the errors. Time to smack yourself in the forehead. Don't worry, we all do it. Yea, I complete missed that, however I was sort of on They still would cause problems. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 26, 2009 Share Posted January 26, 2009 It's not the default values. Remove them and it still does not trigger the errors. Time to smack yourself in the forehead. Don't worry, we all do it. Yea, I complete missed that, however I was sort of on They still would cause problems. Yes, I agree. If you leave the default values in there are going to be a lot of submissions with the email address "example@domain.com". At the very least the validation should check for the default values and treat them as errors. I'd also suggest an option at the top of the select list such as "--Select One--" with an empty value to force the user to select the appropriate value. By the way, the "smack yourself i nthe forehead" was meant for the OP, not you. 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.