Jump to content

help converting old php form


kaiman

Recommended Posts

I am working on convering an old PHP form mail script to do error checking on the same page as the form rather then using redirects and I am having a bit of trouble with the logic. Right now it seems to work okay in general, but will send blank emails without doing the error check. Can someone please help me get it sorted out so that it works correctly. Also any ideas or input on how to make it more secure are appreciated.

 

Thanks in advance,

 

kaiman

 

Here is the section of code in question:

 

// validate form
if(isset($_POST['submit'])){

// check for empty form fields
if (empty($name) || empty($email) || empty($category) || empty($formsubject) || empty($message)) {
	echo "<p>Please complete all required form fields.</p>";
}

// sanitize and validate email address
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL) ;  

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {  
	echo "<p>Please enter a valid email address.</p>";
}

// check for special characters in the message field and reformat
if (get_magic_quotes_gpc()) {
	$message = stripslashes($message);
}
}
if(isset($_POST['email'])){
	// if valid send email
	mail($mailto, $subject, $messageproper,
	"From: \"$name\" <$email>\r\n" . "Reply-To: \"$name\" <$email>\n" . "X-Mailer: PHP 5.2.5" );
	header( "Location: $successurl" );
}
else {
		echo "<!-- begin form -->\n";		
		echo "<form name=\"Contact\" class=\"contentform\" method=\"post\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
		echo "<fieldset>\n";
		echo "<legend>Contact Form</legend>\n";	
		echo "<ol class=\"form\">\n";
		echo "<li class=\"formleft\">\n";
		echo "<label for=\"name\"><span class=\"asterisk\">&#042;</span> Your Name:</label>\n";
		echo "<input class=\"textfield\" id=\"name\" name=\"name\" type=\"text\" value=\"\" />\n";
		echo "</li>\n";
		echo "<li class=\"formright\">\n";
		echo "<label for=\"email\"><span class=\"asterisk\">&#042;</span> Email Address:</label>\n";
		echo "<input class=\"textfield\" name=\"email\" type=\"text\" id=\"email\" value=\"\" />\n";
		echo "</li>\n";
		echo "<li class=\"formleft\">\n";
		echo "<label for=\"category\"><span class=\"asterisk\">&#042;</span> Form Category:</label>\n";
		echo "<select class=\"select\" name=\"category\" id=\"category\" onchange=\"javascript:enableOther();\">\n";
		echo "<option value=\"\">Please Select an Option:</option>\n";
		echo "<option value=\"Question\" >Question</option>\n";
		echo "<option value=\"Comment\" >Comment</option>\n";
		echo "<option value=\"Idea\">Idea</option>\n";
		echo "<option value=\"Other\">Other</option>\n";
		echo "</select>\n";
		echo "</li>\n";
		echo "<li class=\"formright\">\n";
		echo "<label for=\"formsubject\"><span class=\"asterisk\">&#042;</span> Form Subject:</label>\n";
		echo "<input class=\"textfield\" name=\"formsubject\" type=\"text\" id=\"formsubject\" value=\"\" />\n";
		echo "</li>\n";
		echo "<li>\n";
		echo "<label for=\"message\"><span class=\"asterisk\">&#042;</span> Your Message:</label>\n";
		echo "<textarea name=\"message\" class=\"textarea\" rows=\"5\" cols=\"20\" id=\"message\" value=\"\"></textarea>\n";
		echo "</li>\n";
		echo "<li>\n";
		echo "<label for=\"submitbutton\"></label>\n";
		echo "<button class=\"submitbutton\" type=\"submit\" name=\"submit\" title=\"Submit\">Submit</button>\n";
		echo "</li>\n";
		echo "</ol>\n";	
		echo "</fieldset>\n";
		echo "</form>\n";
		echo "<!-- end form -->\n";
	}	

 

Link to comment
Share on other sites

UPDATE:

 

Here is what I have now, which seems to perform the error checks but won't send emails (complete or incomplete). Any suggestions are greatly appreciated.

 

kaiman

 

// validate form
if(isset($_POST['submit'])){

// check for empty form fields
if (empty($name) || empty($email) || empty($category) || empty($formsubject) || empty($message)) {
	echo "<p>Please complete all required form fields.</p>";
}

// sanitize and validate email address
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL) ;  

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {  
	echo "<p>Please enter a valid email address.</p>";
}

// check for special characters in the message field and reformat
if (get_magic_quotes_gpc()) {
	$message = stripslashes($message);
}
else {
// if valid send email
mail($mailto, $subject, $messageproper,
"From: \"$name\" <$email>\r\n" . "Reply-To: \"$name\" <$email>\n" . "X-Mailer: PHP 5.2.5" );
header( "Location: $successurl" );	
}
}
		echo "<!-- begin form -->\n";		
		echo "<form name=\"Contact\" class=\"contentform\" method=\"post\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
		echo "<fieldset>\n";
		echo "<legend>Contact Form</legend>\n";	
		echo "<ol class=\"form\">\n";
		echo "<li class=\"formleft\">\n";
		echo "<label for=\"name\"><span class=\"asterisk\">&#042;</span> Your Name:</label>\n";
		echo "<input class=\"textfield\" id=\"name\" name=\"name\" type=\"text\" value=\"\" />\n";
		echo "</li>\n";
		echo "<li class=\"formright\">\n";
		echo "<label for=\"email\"><span class=\"asterisk\">&#042;</span> Email Address:</label>\n";
		echo "<input class=\"textfield\" name=\"email\" type=\"text\" id=\"email\" value=\"\" />\n";
		echo "</li>\n";
		echo "<li class=\"formleft\">\n";
		echo "<label for=\"category\"><span class=\"asterisk\">&#042;</span> Form Category:</label>\n";
		echo "<select class=\"select\" name=\"category\" id=\"category\" onchange=\"javascript:enableOther();\">\n";
		echo "<option value=\"\">Please Select an Option:</option>\n";
		echo "<option value=\"Question\" >Question</option>\n";
		echo "<option value=\"Comment\" >Comment</option>\n";
		echo "<option value=\"Idea\">Idea</option>\n";
		echo "<option value=\"Other\">Other</option>\n";
		echo "</select>\n";
		echo "</li>\n";
		echo "<li class=\"formright\">\n";
		echo "<label for=\"formsubject\"><span class=\"asterisk\">&#042;</span> Form Subject:</label>\n";
		echo "<input class=\"textfield\" name=\"formsubject\" type=\"text\" id=\"formsubject\" value=\"\" />\n";
		echo "</li>\n";
		echo "<li>\n";
		echo "<label for=\"message\"><span class=\"asterisk\">&#042;</span> Your Message:</label>\n";
		echo "<textarea name=\"message\" class=\"textarea\" rows=\"5\" cols=\"20\" id=\"message\" value=\"\"></textarea>\n";
		echo "</li>\n";
		echo "<li>\n";
		echo "<label for=\"submitbutton\"></label>\n";
		echo "<button class=\"submitbutton\" type=\"submit\" name=\"submit\" title=\"Submit\">Submit</button>\n";
		echo "</li>\n";
		echo "</ol>\n";	
		echo "</fieldset>\n";
		echo "</form>\n";
		echo "<!-- end form -->\n";

Link to comment
Share on other sites

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.