Tech Support Guy banner
Status
Not open for further replies.

HTML Contact Form

3K views 26 replies 3 participants last post by  Dingus 
#1 ·
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.
 
#5 ·
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 = 'jondoe@gmail.com';
$email_subject = "New Form submission";
$email_body = "You have received a new message from $name.\n".
"Here is the message:\n $message".
?>
 
#8 ·
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
 
#11 ·
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="jonDoe@gmail.com";
$siteemailtoreceive="jonDoe@gmail.com";

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: jonDoe@gmail.com'))) {
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.
 
#12 ·
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="jondoe@gmail.com";
$siteemailtoreceive="jondoe@gmail.com";
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: jondoe@gmail.com'))) {
header("Location: ".$goto_after_mail);
$success = "Your message has been sent. We shall reply as soon as we can";
}
?>
 
#13 ·
Strip out the validation and see if it works so try this

PHP:
<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);
$siteemailtosend="jondoe@gmail.com";
$siteemailtoreceive="jondoe@gmail.com";

$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: jondoe@gmail.com'))) {
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
 
#14 ·
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";

Font Parallel Rectangle Screenshot Number
 
#17 ·
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
 
#18 ·
Is the html on a separate page? If so then put it all on one page so try something like:

PHP:
?<php
    //my email form
    ?>

First Name

Last Name

Email Address

Telephone

Message

<?php

$siteemailtosend="jonDoe@gmail.com";
$siteemailtoreceive="jonDoe@gmail.com";

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: jonDoe@gmail.com'))) {
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.
 
#22 ·
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="jondoe@gmail.com";
$siteemailtoreceive="jondoe@gmail.com";

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: jondoe@gmail.com'))) {
header("Location: ".$goto_after_mail);
$success = "Your message has been sent. We shall reply as soon as we can";
}

?>
 
#24 ·
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:
<?php
//my email form
?>

First Name

Last Name

Email Address

Telephone

Message

<?php

$siteemailtosend="[EMAIL]jondoe@gmail.com[/EMAIL]";
$siteemailtoreceive="[EMAIL]jondoe@gmail.com[/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]jondoe@gmail.com[/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.
 
#25 ·
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 = "youremail@here.com";
$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.
 
#27 ·
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.
 
Status
Not open for further replies.
You have insufficient privileges to reply here.
Top