Inserting PHP into Sharepoint - php

I have created a 'Contact Us' form for a SharePoint for a group in work. I created this using HTML and have created a PHP script for the backend to send the form to my email address. How do I go about inserting this PHP into SharePoint so that this works? Below is the HTML and PHP code.
<span style="color: #00cc99; font-size: 15pt;"><span style="color: #00cc99;">Contact Us if you're interested in volunteering with Social Innovation. </span></span></p>
<table width="450">
<tbody>
<tr>
<td valign="top">
<label>Name *</label> </td>
<td valign="top">
<input type="text" size="30" maxlength="50"/>
</td>
</tr>
<tr>
<td valign="top">
<label>Email Address *</label> </td>
<td valign="top">
<input type="text" size="30" maxlength="80"/>
</td>
</tr>
<tr>
<td valign="top">
<label>Message *</label> </td>
<td valign="top">
<textarea maxlength="1000" rows="6" cols="25"></textarea> </td>
</tr>
<tr>
<td colspan="2" style="text-align: center;">
<input type="button" value="Submit"/>
</td>
</tr>
</tbody>
</table>
<p> </p>
<?php
if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW TO SUSANNAH
$email_to = "example#email.co.uk";
$email_subject = "Sharepoint Volunteer Enquiry";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
//thank you message
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
die();
?>

Sharepoint Site using PHP code
I did it the same way like they suggest in this post using the Page Viewer WebPart.
But as this post is 5 years old, there might be a free php-viewer Webpart by now (as there is a HTML-Viewer)

Related

Email not receiving in php form although it has been submitted [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I was making a html form and when ever someone submit after filling info in it , it should go to my email "nishancofficial#gmail.com". I uploaded my code into website www.nishan.ga/form.html and i fill information for testing it and when I click Submit it say "Submitted" but I waited for long time but it didn't arrived to my gmail address. Can anyone help me!
This is form.html page
<form name="contactform" method="post" action="send.php">
<table width="450px">
<tr>
<td valign="top">
<label for="first_name">First Name *</label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top"">
<label for="last_name">Last Name *</label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email">Email Address *</label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="comments">Comments *</label>
</td>
<td valign="top">
<textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
This is send.php page
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "nishancofficial#gmail.com";
$email_subject = "User Email";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
THANK you for contacting us. We will be in touch with you very soon.
<?php
}
?>
What am i missing here.
Your code does work. However, I did notice a small error in your posted code in the form.html. There is an extra " at line 13.
There are other questions on StackOverflow regarding PHP Mail not working you'll need to make sure you have a mail server installed on your server (localhost or other) that allows you to send email with the mail function.

HTML Form / PHP

I am making a website and have coded a form for users to fill out and then the form should be emailed to john.doe#gmail.com. But when I upload the files on to the server, and test the form with correct details, it does not work. The html code is as follows:
<form class="form" name="htmlform" method="post" action="send2.php">
<table width="540px">
</tr>
<tr>
<td valign="top">
<label for="name">Name *</label>
</td>
<td valign="top">
<input type="text" name="name" maxlength="70" size="30">
</td>
</tr>
<tr>
<td valign="top"">
<label for="phone">Cell Phone *</label>
</td>
<td valign="top">
<input type="text" name="phone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email">Email *</label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="30" size="30">
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
And then there is an external php file named "send2.php" which contains:
$email_to = "john.doe#gmail.com";
$email_subject = "Feedback Form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['phone']) ||
!isset($_POST['email'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$phone = $_POST['phone']; // required
$email_from = $_POST['email']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
$cell_exp = "/^[0-9 .'-]+$/";
if(!preg_match($cell_exp,$phone)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Phone: ".clean_string($phone)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
// creating email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
die();
?>
Could somebody please examine this code and tell me where do I go wrong. Any help would be extremely appreciated.
Thanks,
WS
Could you please be more specific what error are you getting ?
Also you could try using !empty () or
if(!isset() && !empty ()) {}
instead of isset

Sending email from PHP is not working

I'll start off by saying that I know absolutely nothing about PHP. I'm more .NET.
I pulled this email template off the Internet - and the instructions said to simply change the "To" email address and the "Subject" line to whatever I wanted. At the very end of my PHP file, I have "Thank you for contacting us. We will be in touch with you very soon." - this message gets displayed on my browser when I click Submit. It thinks for a second, then displays the message, kind of indicating to me that the email sent. But I don't get any email. Any ideas why?
Here's my HTML form code:
<form name="contactform" method="post" action="MailHandler.php">
<table width="450px">
<tr>
<td valign="top">
<label for="first_name">First Name *</label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="last_name">
Last Name *</label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email">Email Address *</label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone">Telephone Number</label>
</td>
<td valign="top">
<input type="text" name="telephone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="comments">Comments *</label>
</td>
<td valign="top">
<textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<br/>
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
Here's MailHandler.php:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "mike#mikemarks.net";
$email_subject = "Your email subject line";
function died($error) {
// Your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// Validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // Required
$last_name = $_POST['last_name']; // Required
$email_from = $_POST['email']; // Required
$telephone = $_POST['telephone']; // Not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// Create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- Include your own success HTML here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
The code looks legitimate. Ask if your host firm supports PHP-powered email...
I have used emails myself in PHP. The variables are automatically passed from my HTML file to what you are calling your mailHandler.php file. He is a sample of how I used the mail system. I hope it is helpful.
<?php
$to = $_POST['emailaddress'];
$subject = 'subject here';
$message = '<html><head><style type="text/css">Insert any css here</style></head><body>';
$message .= 'Any HTML like you were designing a standard screen display';
$from = 'emailaddress';
$headers = 'MIME-Version: 1.0' . "\r\n';
$headers .= "Content-type: text/html; charset=iso=8859-1' . "\r\n";
$headers .= "From: $email_from";
mail ($to,$subject,$message,$headers);
echo "<input type='button' value=\"Click to Close \" onclick=\"window.open('urlHere.php','_self');\" />";
?>
This is a simplistic version but it allows you to create/send an email that emulates a website within the email itself. Simply add any HTML code line by line inside the additional $message .= ' '; section.
I hope this was helpful. It is how I do emails.

contact form php [duplicate]

This question already has answers here:
Why are my PHP files showing as plain text? [duplicate]
(7 answers)
Closed 10 years ago.
This is driving me crazy because I had this form before and it did work and now I am not able to make it work.
The problem is that when I press the button "Submit" instead of submit the form, the "ReportIssueForm.php" file opens as a txt file in the browser and I see all the code.
Any help would be highly appreciated.
Here is the HTML code to call the PHP file:
<form name="htmlform" method="post" action="ReportIssueForm.php">
<table width="450px">
</tr>
<tr>
<td valign="top">
<label for="first_name"><b>First Name *</b></label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top"">
<label for="last_name"><b>Last Name *</b></label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email"><b>Email *<font size='2'></b></label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="comments"><b>Comments*</b></label>
</td>
<td valign="top">
<textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>
</td>
</tr>
<tr>
<td colspan="4" style="text-align:center">
<br><br>
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
And here is the code from the php file:
<?php
if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "myemail";
$email_subject = "Reporting and issue";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- place your own success html below -->
Thank you for Reporting an issue. We will look into it and try to solve it as soon as possible.<br><br>
<?php
}
die();
?>
You need to check configuration, if your web-server is configured properly to route .php files request to PHP engine.
In this scenario it seems web-server might not be configured properly.
If you are using Apache as web-server, Make sure below 2 lines are un-commented in httpd.conf(apache config fle)
LoadModule phpX_module "d:/wamp/bin/php/phpXXX/php5apacheXX.dll"
AddType application/x-httpd-php .php
Where X means PHP version

Print message above PHP contact form

Hey all, I have a basic HTML contact form that goes to a .php file after the submit button is clicked. At the moment, if there are no errors when a user presses submit, it brings them to a separate page saying "Thank you for contacting us...". Would it be possible to print that message above the contact form when they click submit? I would really like them to just stay on the current page instead of getting redirected.
Here's the HTML form:
<form name="contactform" method="post" action="scripts/contactform.php">
<table width="450px">
</tr>
<tr>
<td valign="top">
<label for="first_name"><b>Name *</b></label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email"><b>Email Address *</b></label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone"><b>Telephone</b></label>
</td>
<td valign="top">
<input type="text" name="telephone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="message">Message: *</label>
</td>
<td valign="top">
<textarea name="message" maxlength="1000" cols="40" rows="8"></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit" style="height:35px; width:90px;">
</td>
</tr>
</table>
</form>
Here is the contactform.php file:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email#domain.com";
$email_subject = "Contact Form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['Telephone']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['message']; // required
$error_message = "";
$email_exp = "^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$";
if(!eregi($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "^[a-z .'-]+$";
if(!eregi($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!eregi($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Message: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- Success HTML -->
Thank you for contacting us. We will get back to you as soon as possible.
<?
}
?>
Any ideas on what I should do?
You would need to use AJAX. Here is an example of what you want to do:
http://www.bitrepository.com/a-simple-ajax-contact-form-with-php-validation.html
<?php
$flag = 0;
if(isset($_POST['email'])) {
$flag=1;
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email#domain.com";
$email_subject = "Contact Form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['Telephone']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['message']; // required
$error_message = "";
$email_exp = "^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$";
if(!eregi($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "^[a-z .'-]+$";
if(!eregi($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!eregi($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Message: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- Success HTML -->
Thank you for contacting us. We will get back to you as soon as possible.
<?
}
?>
<?php
if($flag==0)
{
?>
<form name="contactform" method="post" action="scripts/contactform.php">
<table width="450px">
</tr>
<tr>
<td valign="top">
<label for="first_name"><b>Name *</b></label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email"><b>Email Address *</b></label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone"><b>Telephone</b></label>
</td>
<td valign="top">
<input type="text" name="telephone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="message">Message: *</label>
</td>
<td valign="top">
<textarea name="message" maxlength="1000" cols="40" rows="8"></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit" style="height:35px; width:90px;">
</td>
</tr>
</table>
</form>
<?php
}
?>
The best thing to do would be to have your contact form as a PHP file so that it can conditionally display the error message.
You can do that in one of two ways:
Have the contactform.php file redirect back to the HTML form if an error occurs. In this case, you'll have to save the error message to a session variable.
Include all the form processing at the top of the HTML form, and have the form's action point to itself.
Then, right above the form, just include something like
<?php if (!empty($error_message)): ?>
<p class="error"><?php echo $error_message; ?></p>
<?php endif; ?>

Categories