# Beginner Help! Error On Contact Form PHP

**URL:** <https://www.phphelp.com/t/beginner-help-error-on-contact-form-php/20367>\
**Category:** General PHP Help\
**Created:** [May 29, 2013, 12:35am UTC](https://www.phphelp.com/t/beginner-help-error-on-contact-form-php/20367 "2013-05-29T00:35:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![isaacmartin](https://www.phphelp.com/letter_avatar_proxy/v4/letter/i/b782af/32.png) [@isaacmartin](https://www.phphelp.com/u/isaacmartin)\
**Post date:** [May 29, 2013, 12:35am UTC](https://www.phphelp.com/t/beginner-help-error-on-contact-form-php/20367/1 "2013-05-29T00:35:41Z")

</div>

Hey guys, first post here. Very new to PHP. Have created a contact form with html and the PHP code to verify the form is giving me an error when on the website. [http://www.solarwest.com.au/contact.html](http://www.solarwest.com.au/contact.html)

The error after submitting the form is : Parse error: syntax error, unexpected T\_STRING in /home/solarwee/public\_html/send\_form\_email.php on line 4

Can somebody have a look at the code and see if there is any errors there?

[php]\<?php  
if(isset($\_POST[‘email’])) {

```
$email_to = "info@solarwest.com.au";
$email_subject = "Website 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['telephone']) ||
    !isset($_POST['message'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');      
}
 
$first_name = $_POST['name']; // required

$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_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.  
’;  
}  
$string\_exp = “/^[A-Za-z .’-]+$/”;  
if(!preg\_match($string\_exp,$name)) {  
$error\_message .= ‘The First Name you entered does not appear to be valid.  
’;  
}  
if(strlen($message) \< 2) {  
$error\_message .= ‘The Message you entered do not appear to be valid.  
’;  
}  
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($name)."\n";

$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Message: ".clean_string($messag)."\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 for contacting us. We will be in touch with you very soon.

\<?php } ?\>

[/php]

---

<div class="post-metadata">

**Author:** ![Merely](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/f9ae1b/32.png) [@Merely](https://www.phphelp.com/u/Merely)\
**Post date:** [May 29, 2013, 1:16am UTC](https://www.phphelp.com/t/beginner-help-error-on-contact-form-php/20367/2 "2013-05-29T01:16:01Z")

</div>

[php] echo “We are very sorry, but there were error(s) found with the form you submitted. “;  
echo “These errors appear below.  
  
”;  
echo $error.”  
  
”;  
echo “Please go back and fix these errors.  
  
”;  
die();  
}[/php]

Have you defined the variable, $error, elsewhere in the script? I think that may be a possible problem.

---

<div class="post-metadata">

**Author:** ![isaacmartin](https://www.phphelp.com/letter_avatar_proxy/v4/letter/i/b782af/32.png) [@isaacmartin](https://www.phphelp.com/u/isaacmartin)\
**Post date:** [May 29, 2013, 5:03am UTC](https://www.phphelp.com/t/beginner-help-error-on-contact-form-php/20367/3 "2013-05-29T05:03:04Z")

</div>

Thank’s for your reply, have sorted it.  
One more question though. If i want to redirect back to the homepage after submitting the form what code would i Use for that?

---

<div class="post-metadata">

**Author:** ![awl19](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@awl19](https://www.phphelp.com/u/awl19)\
**Post date:** [May 29, 2013, 9:35pm UTC](https://www.phphelp.com/t/beginner-help-error-on-contact-form-php/20367/4 "2013-05-29T21:35:27Z")

</div>

Hopefully I’m not giving you bad advice. There’s 3 ways I know of doing it.

HTML

```auto
<META http-equiv=”refresh” content=”8;URL=index.php”>
```

JavaScript

`setTimeout(function(){
  window.location = "index.php";
}, 8000);`

PHP  
[php]header(‘Refresh: 8; URL=index.php’);[/php]

8 (or 8000 in JS) = 8 seconds before page redirect  
index.php = page redirect destination

Hope this is what you were looking for.
