# HTML Contact Form



## Dingus (Apr 21, 2002)

Hi folks.
I'm looking for some code for a contact form for my website, or a site that does this coding.
I did find a site that constructed a form a while ago but this has now stopped working.


----------



## SpywareDr (Mar 31, 2014)

https://www.w3schools.com/howto/howto_css_contact_form.asp


----------



## Dingus (Apr 21, 2002)

Thanks SpywareDr. 
This is perfect for me. I only need to do a few tweaks. 
Then work out how pho gets it submitted.


----------



## SpywareDr (Mar 31, 2014)




----------



## Dingus (Apr 21, 2002)

OK folks.
I now have a form (Thanks to SpywareDR) that does what I need, but when I get the info sent to my website I get this error message.

( ! ) Parse error: syntax error, unexpected '?> ' in D:\wamp64\www\EmailForm\action_page.php on line _12_ 
Line 12 only has ?> in it, which I thought just closed the PHP.

This is my action_page.php file:

<!doctype html>

<?php
$email_from = '[email protected]';
$email_subject = "New Form submission";
$email_body = "You have received a new message from $name.\n".
"Here is the message:\n $message".
?>


----------



## SpywareDr (Mar 31, 2014)

Is PHP installed and working?


----------



## colinsp (Sep 5, 2007)

The full stop after the message and before ?> should be a semi colon.


----------



## Dingus (Apr 21, 2002)

Hi SpywareDr. I installed WAMP a while ago, whch has PHP in it. I assumed it would work ok but have never checked it as PHP I know very little of PHP.

colinsp I'll give that a try.

Cheers for now


----------



## Dingus (Apr 21, 2002)

Update for SpywareDr.
PHP is working ok


----------



## SpywareDr (Mar 31, 2014)




----------



## Dingus (Apr 21, 2002)

Boy is this PHP is blowing my mind.
I restarted my attempts to get my contact form working.
When I fill out the form and hit submit I get the error page. "OOPS"

My html, which works fine, I think, is:

First Name

Last Name

Email Address

Telephone

Message

My PHP, which I've borrowed and tweeked, is:

<?php

$siteemailtosend="[email protected]";
$siteemailtoreceive="[email protected]";

if(empty($_POST['fname']) ||
empty($_POST['lname']) ||
empty($_POST['emailadd']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "Opps,!";
return false;
}

$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
// Create the email and send the message
$to = $siteemailtoreceive;
$email_subject = "Website Contact From: $name";
$email_body = "You have received a new message from your website contact form.\n\r\n";
$email_body = $email_body."Here are the details:\n\r\n";
$email_body = $email_body."Name: $fname\n\r\n";
$email_body = $email_body."Last Name: $lname\n\r\n";
$email_body = $email_body."Email: $emailadd\n\r\n";
$email_body = $email_body."Phone: $phone\n\r\n";
$email_body = $email_body."Message: $message\n\n\r\n";
$headers = "From: $siteemailtosend\n";
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers);
return true;

if ((mail($to,$subject,$message,$headers))&&(mail($autoTo, "Thank You from ", $autoreply, 'From: [email protected]'))) {
header("Location: ".$goto_after_mail);
$success = "Your message has been sent. We shall reply as soon as we can";
}

?>

Please remember, my PHP knowledge can be written on a pin head.


----------



## Dingus (Apr 21, 2002)

Been a few days since my last post but finally back to this problem.
As this is my first delv into PHP I'm finding it less than easy.
I've worn my Google keys out but still can't fix this.
If anyone can see a fault in my latest try !!!!!
As per SpywareDr I changed some entries to (I think) match the form and action_page, but no luck.

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);
$siteemailtosend="[email protected]";
$siteemailtoreceive="[email protected]";
if(empty($_POST['fname']) ||
empty($_POST['lname']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))

{
echo nl2br ("\n\n We're really sorry, but the form doesn't seem to be working. ");
return false;
}
$fname = strip_tags(htmlspecialchars($_POST['fname']));
$lname = strip_tags(htmlspecialchars($_POST['lname']));
$email = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
$to = $siteemailtoreceive;
$email_subject = "Website Contact From: $name";
$email_body = "You have received a new message from your website contact form.\n\r\n";
$email_body = $email_body."Here are the details:\n\r\n";
$email_body = $email_body."Name: $fname\n\r\n";
$email_body = $email_body."Last Name: $lname\n\r\n";
$email_body = $email_body."Email: $email\n\r\n";
$email_body = $email_body."Phone: $phone\n\r\n";
$email_body = $email_body."Message: $message\n\n\r\n";
$headers = "From: $siteemailtosend\n";
$headers .= "Reply-To: $email_address\n";
mail($to,$email_subject,$email_body,$headers);
return true;
if ((mail($to,$subject,$message,$headers))&&(mail($autoTo, "Thank You from Pine Lodge", $autoreply, 'From: [email protected]'))) {
header("Location: ".$goto_after_mail);
$success = "Your message has been sent. We shall reply as soon as we can";
}
?>


----------



## colinsp (Sep 5, 2007)

Strip out the validation and see if it works so try this


```
<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);
$siteemailtosend="[email protected]";
$siteemailtoreceive="[email protected]";

$fname = strip_tags(htmlspecialchars($_POST['fname']));
$lname = strip_tags(htmlspecialchars($_POST['lname']));
$email = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
$to = $siteemailtoreceive;
$email_subject = "Website Contact From: $name";
$email_body = "You have received a new message from your website contact form.\n\r\n";
$email_body = $email_body."Here are the details:\n\r\n";
$email_body = $email_body."Name: $fname\n\r\n";
$email_body = $email_body."Last Name: $lname\n\r\n";
$email_body = $email_body."Email: $email\n\r\n";
$email_body = $email_body."Phone: $phone\n\r\n";
$email_body = $email_body."Message: $message\n\n\r\n";
$headers = "From: $siteemailtosend\n";
$headers .= "Reply-To: $email_address\n";
mail($to,$email_subject,$email_body,$headers);
return true;
if ((mail($to,$subject,$message,$headers))&&(mail($autoTo, "Thank You from Pine Lodge", $autoreply, 'From: [email protected]'))) {
header("Location: ".$goto_after_mail);
$success = "Your message has been sent. We shall reply as soon as we can";
}
?>
```
If this works then the problem is narrowed down


----------



## Dingus (Apr 21, 2002)

Hi Colin. Worse than before.
All seems to be related to:

$fname = strip_tags(htmlspecialchars($_POST['fname'])); THIS IS LINE 10
$lname = strip_tags(htmlspecialchars($_POST['lname']));
$email = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
$to = $siteemailtoreceive;
$email_subject = "Website Contact From: $name";


----------



## colinsp (Sep 5, 2007)

Actually its not it tells us what the problem is. None of your inputs have values. So in fact the validation code that I stripped out was doing what it should, saying that those input variables were empty. So the issue is with your input form not this php.


----------



## Dingus (Apr 21, 2002)

excellent Colin. 
I’ll start having a look there.


----------



## Dingus (Apr 21, 2002)

I've had a look and check then checked again. I just can't see a problem with the HTML.
I've posted my latest here.

*Contact Form*

First Name

Last Name

Email Address

Telephone Number

Message


----------



## colinsp (Sep 5, 2007)

Is the html on a separate page? If so then put it all on one page so try something like:


```
?<php
    //my email form
    ?>

First Name

Last Name

Email Address

Telephone

Message

<?php

$siteemailtosend="[email protected]";
$siteemailtoreceive="[email protected]";

if(empty($_POST['fname']) ||
empty($_POST['lname']) ||
empty($_POST['emailadd']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "Opps,!";
return false;
}

$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
// Create the email and send the message
$to = $siteemailtoreceive;
$email_subject = "Website Contact From: $name";
$email_body = "You have received a new message from your website contact form.\n\r\n";
$email_body = $email_body."Here are the details:\n\r\n";
$email_body = $email_body."Name: $fname\n\r\n";
$email_body = $email_body."Last Name: $lname\n\r\n";
$email_body = $email_body."Email: $emailadd\n\r\n";
$email_body = $email_body."Phone: $phone\n\r\n";
$email_body = $email_body."Message: $message\n\n\r\n";
$headers = "From: $siteemailtosend\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;

if ((mail($to,$subject,$message,$headers))&&(mail($autoTo, "Thank You from ", $autoreply, 'From: [email protected]'))) {
header("Location: ".$goto_after_mail);
$success = "Your message has been sent. We shall reply as soon as we can";
}

?>
```
give the file a .php extension and try that. I haven't tested this.


----------



## Dingus (Apr 21, 2002)

Yes Colin I have them on two different pages.
I'll give that a go.
Cheers


----------



## colinsp (Sep 5, 2007)

Dingus said:


> Yes Colin I have them on two different pages.
> I'll give that a go.
> Cheers


I just noticed a typo in my code which hopefully you picked up the opening line should of course be <?php


----------



## Dingus (Apr 21, 2002)

Thanks Colin. 
I didn’t but Dreamweaver did. 😂


----------



## Dingus (Apr 21, 2002)

Some development.
While I no longer get a fault when I have all the code on one page, nothing happens when I submit the email form. The fields are blanked, as in a page refresh, but no email is sent.
Some progress is better then none.

My complete code including style:

<?php
//my email form
?>

First Name

Last Name

Email Address

Telephone

Message

<?php

$siteemailtosend="[email protected]";
$siteemailtoreceive="[email protected]";

if(empty($_POST['fname']) ||
empty($_POST['lname']) ||
empty($_POST['emailadd']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "Opps,!";
return false;
}

$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
// Create the email and send the message
$to = $siteemailtoreceive;
$email_subject = "Website Contact From: $name";
$email_body = "You have received a new message from your website contact form.\n\r\n";
$email_body = $email_body."Here are the details:\n\r\n";
$email_body = $email_body."Name: $fname\n\r\n";
$email_body = $email_body."Last Name: $lname\n\r\n";
$email_body = $email_body."Email: $emailadd\n\r\n";
$email_body = $email_body."Phone: $phone\n\r\n";
$email_body = $email_body."Message: $message\n\n\r\n";
$headers = "From: $siteemailtosend\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;

if ((mail($to,$subject,$message,$headers))&&(mail($autoTo, "Thank You from ", $autoreply, 'From: [email protected]'))) {
header("Location: ".$goto_after_mail);
$success = "Your message has been sent. We shall reply as soon as we can";
}

?>


----------



## Dingus (Apr 21, 2002)

I just discovered, if I leave one of the fields empty I don't get a fault.


----------



## colinsp (Sep 5, 2007)

It helps if you post all your code within code tags (three dots in the icons above).

As this is a php page the first code on a page must be the <?php so


```
<?php
//my email form
?>

First Name

Last Name

Email Address

Telephone

Message

<?php

$siteemailtosend="[EMAIL][email protected][/EMAIL]";
$siteemailtoreceive="[EMAIL][email protected][/EMAIL]";

if(empty($_POST['fname']) ||
empty($_POST['lname']) ||
empty($_POST['emailadd']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "Opps,!";
return false;
}

$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
// Create the email and send the message
$to = $siteemailtoreceive;
$email_subject = "Website Contact From: $name";
$email_body = "You have received a new message from your website contact form.\n\r\n";
$email_body = $email_body."Here are the details:\n\r\n";
$email_body = $email_body."Name: $fname\n\r\n";
$email_body = $email_body."Last Name: $lname\n\r\n";
$email_body = $email_body."Email: $emailadd\n\r\n";
$email_body = $email_body."Phone: $phone\n\r\n";
$email_body = $email_body."Message: $message\n\n\r\n";
$headers = "From: $siteemailtosend\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;

if ((mail($to,$subject,$message,$headers))&&(mail($autoTo, "Thank You from ", $autoreply, 'From: [EMAIL][email protected][/EMAIL]'))) {
header("Location: ".$goto_after_mail);
$success = "Your message has been sent. We shall reply as soon as we can";
}

?>
```
Again this is untested. It may be easier to just take one of the scripts from sites such as https://1stwebdesigner.com/php-contact-form-html/ or https://phppot.com/php/php-contact-form/ or https://code.tutsplus.com/tutorials/create-a-contact-form-in-php--cms-32314 or one of the many others and that would work out of the box and then just change the input fields to suit due to your lack of programming knowledge.


----------



## Dingus (Apr 21, 2002)

Ok, took onboard the links.
Redone the html but the PHP still won't send an email.
I keep getting 'Failed to connect to mailserver
I have this if anyone can see whet I'm missing:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Call Back: Message: $message";
$recipient = "[email protected]";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

At least it's now trying. Just wont send.


----------



## colinsp (Sep 5, 2007)

Are you running this code on a live server or a local server such as WampServer?

The PHP mail function does *NOT *work on local servers.

If on a live server and it doesn't work then maybe the mail function is disabled. If this is the case then get PHPMailer and install and configure this.


----------



## Dingus (Apr 21, 2002)

Thanks Lisa. 
That was one I tried early on. 
with the guidance of others I’ve found that my issue is not having a mail handler on my server. 
I’m told this should solve the problem. 
Unfortunately i ain’t been able to get back to this yet.


----------

