Jump to content

Sticky forms: There has got to be a more efficient way


Recommended Posts

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?

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

 

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.