kyalee Posted January 22, 2009 Share Posted January 22, 2009 This question isn't so much help me make this work as help me do this better. I updated my contact form to be sticky so that people won't have to reenter their information if they skip a required field or format their email address incorrectly. As of right now, it works fine. But the code makes me itch. It's ugly and it's completely inefficient. //RETREIVING FORMATTING HEADER include('header.inc'); //RETREIVING POST VARIABLES $name = $_POST['name']; $company = $_POST['company']; $email = $_POST['email']; $phone = $_POST['phone']; $comments = $_POST['comments']; //STRIP SLASHES $name = stripslashes($name); $company = stripslashes($company); $comments = stripslashes($comments); //"STICKY FORM"; WILL BE DISPLAYED IF THE USER DOES NOT FILL OUT THE FORM CORRECTLY $form = "<form method=\"post\" action=\"contactform.php\"> <fieldset> <legend>* Indicates a required field.</legend> <p><label for=\"name\">*Name:</label> <input type=\"text\" name=\"name\" id=\"name\" value=\"$name\" /></p> <p><label for=\"company\">Company:</label> <input type=\"text\" name=\"company\" id=\"company\" value=\"$company\" /></p> <p><label for=\"email\">*Email:</label> <input type=\"text\" name=\"email\" id=\"email\" value=\"$email\" /></p> <p><label for=\"phone\">Phone #:</label> <input type=\"text\" name=\"phone\" id=\"phone\" value=\"$phone\" /></p> <p><label for=\"comments\">*Comments:</label> <textarea name=\"comments\" rows=\"10\" cols=\"40\" wrap>$comments</textarea></p> <p class=\"submit\"> <input type=\"submit\" name=\"submit\" type=\"submit\" value=\"Submit\" /> <input type=\"reset\" name=\"reset\" id=\"reset\" value=\"Clear\" /> </p> </fieldset> </form>"; // WHERE THE E-MAIL IS GOING $recipient = 'email@email.com'; //EMAIL SUBJECT $subject = 'Contact Form Submitted'; //WHAT THE EMAIL SAYS $msg = "New Contact Form Submitted with the following information: Name: $name Company: $company Email: $email Phone #: $phone Comments: $comments"; //REQUIRED FIELD ERROR MESSAGE $error_required = "<p>You did not fill in all required fields. Please check the information that you entered below and submit your form again.</p>"; //EMAIL FORMAT ERROR MESSAGE $error_email = "<p>The email address you entered is invalid. Please check the information that you entered below and submit your form again.</p>"; //EMAIL SENT SUCESSFULLY MESSAGE $sucess_msg = "<p>Thank you for your interest. We will contact you with more information as soon as possible.</p>"; //REGULAR EXPRESSION FOR CHECKING CORRECT FORMAT OF EMAIL $emailregexp = "[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})"; //CHECKING THAT INFORMATION HAS BEEN ENTERED IN ALL REQUIRED FIELDS if ((!$name) || (!$email) || (!$comments)) { echo $error_required; echo $form; } //CHECKING THAT EMAIL ADDRESS IS CORRECTLY FORMATTED elseif (!eregi($emailregexp, $email)) { echo $error_email; echo $form; } else { //MAILING FORM mail ($recipient, $subject, $msg, 'From: info@ambercoffee.com'); echo $sucess_msg; } See all those back slashes in the $form variable. Ugly and inefficient. How can I do this better? Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted January 22, 2009 Share Posted January 22, 2009 Please use php tags. Code doesn't need to look pretty and escaping characters is fine. But if you don't want to then you can do $var = <<something stuff something>> that would allow you to insert html without the need of escaping. Or similar, one of the other members may know Quote Link to comment Share on other sites More sharing options...
kyalee Posted January 22, 2009 Author Share Posted January 22, 2009 Please use php tags. You mean "<?php" and "?>"? I use them, I just left them out when I pasted the code to save space. I figured you guys didn't care about all of the comment notes in the beginning of the script. Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted January 22, 2009 Share Posted January 22, 2009 I meant in the code you posted as it has syntax highlighting. Quote Link to comment Share on other sites More sharing options...
kyalee Posted January 22, 2009 Author Share Posted January 22, 2009 Oh, I see. Will use them in the future. Quote Link to comment Share on other sites More sharing options...
Maq Posted January 22, 2009 Share Posted January 22, 2009 I would recommend using tags with "<?php ?>" encapsulating the code rather than just tags because they produce a large amount of line breaks if you CnP it to your IDE. But if you don't want to then you can do $var = stuff something>> that would allow you to insert html without the need of escaping. @kyalee, This is called HEREDOC, incase you're wanting to look it up. 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.