HTML Contact Form

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Dingus

Thread Starter
Joined
Apr 21, 2002
Messages
1,191
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

<label for="fname">First Name</label>
<input type="text" id="fname" name="firstname" placeholder="Your first name..">

<label for="lname">Last Name</label>
<input type="text" id="lname" name="lastname" placeholder="Your last name..">

<label for="email">Email Address</label>
<input type="text" id="email" name="emailadd" placeholder="Your email address..">

<label for="phone">Telephone Number</label>
<input type="text" id="phone" name="phonenumber" placeholder="Your telephone number..">

<label for="message">Message</label>
<textarea id="message" name="messagesent" placeholder="Write your message here.." style="height:200px"></textarea>

<input type="submit" value="Submit">
 

colinsp

Colin
Joined
Sep 5, 2007
Messages
2,365
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
    ?>
<div class="email">
<form  method="post" enctype="multipart/form-data">

<label for="fname">First Name</label>
<input type="text" id="name" name="name" placeholder="Your first name..">

<label for="lname">Last Name</label>
<input type="text" id="lname" name="lastname" placeholder="Your last name..">

<label for="emailadd">Email Address</label>
<input type="text" id="emailadd" name="emailadd" placeholder="Your email address..">

<label for="phone">Telephone</label>
<input type="text" id="phone" name="phone" placeholder="Your telephone number..">

<label for="message">Message</label>
<textarea id="message" name="message" placeholder="Write something.." style="height:200px"></textarea>

<input type="submit" value="Submit">

</form>
</div>

<?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.
 

Dingus

Thread Starter
Joined
Apr 21, 2002
Messages
1,191
Yes Colin I have them on two different pages.
I'll give that a go.
Cheers
 

Dingus

Thread Starter
Joined
Apr 21, 2002
Messages
1,191
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:

<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {font-family: Arial, Helvetica, sans-serif;}
* {box-sizing: border-box;}

input[type=text], select, textarea {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
margin-top: 6px;
margin-bottom: 16px;
resize: vertical;
}

input[type=submit] {
background-color: #196C7E;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}

input[type=submit]:hover {
background-color: #45a049;
}

.email {
border-radius: 40px;
background-color: #9EE6EC;
padding: 20px;
width: 80%;
}
</style>

<?php
//my email form
?>
<div class="email">
<form method="post" enctype="multipart/form-data">

<label for="fname">First Name</label>
<input type="text" id="name" name="name" placeholder="Your first name..">

<label for="lname">Last Name</label>
<input type="text" id="lname" name="lastname" placeholder="Your last name..">

<label for="email">Email Address</label>
<input type="text" id="email" name="email" placeholder="Your email address..">

<label for="phone">Telephone</label>
<input type="text" id="phone" name="phone" placeholder="Your telephone number..">

<label for="message">Message</label>
<textarea id="message" name="message" placeholder="Write something.." style="height:200px"></textarea>

<input type="submit" value="Submit">

</form>
</div>

<?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";
}

?>
 

Dingus

Thread Starter
Joined
Apr 21, 2002
Messages
1,191
I just discovered, if I leave one of the fields empty I don't get a fault.
 

colinsp

Colin
Joined
Sep 5, 2007
Messages
2,365
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
?>   

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {font-family: Arial, Helvetica, sans-serif;}
* {box-sizing: border-box;}

input[type=text], select, textarea {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
margin-top: 6px;
margin-bottom: 16px;
resize: vertical;
}

input[type=submit] {
background-color: #196C7E;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}

input[type=submit]:hover {
background-color: #45a049;
}

.email {
border-radius: 40px;
background-color: #9EE6EC;
padding: 20px;
width: 80%;
}
</style>
    </head>
    <body>
<div class="email">
<form method="post" enctype="multipart/form-data">

<label for="fname">First Name</label>
<input type="text" id="name" name="name" placeholder="Your first name..">

<label for="lname">Last Name</label>
<input type="text" id="lname" name="lastname" placeholder="Your last name..">

<label for="email">Email Address</label>
<input type="text" id="email" name="email" placeholder="Your email address..">

<label for="phone">Telephone</label>
<input type="text" id="phone" name="phone" placeholder="Your telephone number..">

<label for="message">Message</label>
<textarea id="message" name="message" placeholder="Write something.." style="height:200px"></textarea>

<input type="submit" value="Submit">

</form>
</div>
</body>
<?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.
 

Dingus

Thread Starter
Joined
Apr 21, 2002
Messages
1,191
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.
 

colinsp

Colin
Joined
Sep 5, 2007
Messages
2,365
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

Thread Starter
Joined
Apr 21, 2002
Messages
1,191
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
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top