1

I am making a website and I am wanting to setup a contact email form. Something like this: contact email form image

I am lead to understand that this code will do that? But when I click the submit button nothing happens. I have never used php before so I have no idea what I am doing wrong.

<?php 
if(isset($_POST['submit'])){
    $to = "mecinoart@hotmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    }
?>

<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<style>
#from{
 font-weight: bold;
 font-family: Trajan Pro;
 font-size: 14px;
 color: #66441c;
 }
.input{
 outline: none;
 margin: 5px 0 10px 0;
 padding-left: 10px;
 width: 230px;
 height: 35px;
 font-family: Adobe Garamond Pro;
 font-size: 14px;
 color: #A4A4A4;
 border-radius: 5px;
 border: 1px solid #a37f4f;
 }
textarea {
 padding-top: 10px;
 } 
#message{
 outline: none;
 margin: 5px 0 10px 0;
 padding-left: 10px;
 width: 230px;
 height: 60px;
 font-family: Adobe Garamond Pro;
 font-size: 14px;
 color: #A4A4A4;
 border-radius: 5px;
 border: 1px solid #a37f4f;
 } 
#button{
 margin-top: -10px;
 border: none;
 outline: none;
 border-radius: 5px;
 width: 230px;
 height: 35px;
 font-family: Trajan Pro;
 font-size: 14px;
 color: #fff;
 background-color: #a37f4f;
 }
#button:hover {
  background-color: #401b0f;
  color: #a37f4f;
}  
</style>
<body>

<form id="from" action="" method="post">
Name <input class="input" type="text" name="first_name"><br>
Email <input class="input" type="text" name="email"><br>
Message <br><textarea id="message" rows="5" name="message" cols="30"></textarea><br>
<input id="button" type="submit" name="submit" value="Submit">
</form>

</body>
</html> 
Cleo
  • 11
  • 1
  • There is no success message displayed and no error checking or reporting so you would not receive an error if it failed. The only way to confirm would be if you received the email. There are various things you can try once you narrow down what is happening. – Tristan Nov 14 '15 at 08:23

4 Answers4

0

Try to check if the email is sent or not.

<? php
if (isset($_POST['submit'])) {
  $to = "mecinoart@hotmail.com"; // this is your Email address
  $from = $_POST['email']; // this is the sender's Email address
  $first_name = $_POST['first_name'];
  $subject = "Form submission";
  $subject2 = "Copy of your form submission";
  $message = $first_name.
  " ".$last_name.
  " wrote the following:".
  "\n\n".$_POST['message'];
  $message2 = "Here is a copy of your message ".$first_name.
  "\n\n".$_POST['message'];

  $headers = "From:".$from;
  $headers2 = "From:".$to;
  $email1 = mail($to, $subject, $message, $headers);
  $email2 = mail($from, $subject2, $message2, $headers2); // sends a copy of the message to the sender
}
if ($email1) {// checking if email 1 is sent
  echo "Email 1 is sent";
} else {
  echo "Email 1 is not sent";
}
if ($email2) {//checking if email 2 is sent
  echo "Email 2 is sent";
} else {
  echo "Email 2 is not sent";
} ?>

add this to your form attribute. action="<?php echo $_SERVER['PHP_SELF']; ?>" example: <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">

Mark Vincent Manjac
  • 507
  • 1
  • 6
  • 27
0

Your form's action and onsubmit attributes are blank, so the form information is never submitted anywhere. You need to either specify what page to send the form's contents to (using action="...") or specify a javascript function to execute (using onsubmit="..."), otherwise nothing will happen when you click the Submit button.

Darwin von Corax
  • 5,201
  • 3
  • 17
  • 28
0

I had similar issue

I used in form Tag

action="<?php echo $_SERVER['PHP_SELF']; ?>" 
Ashish
  • 1,856
  • 18
  • 30
0

Why you use $last_name? Either remove it or add one new textbox for last name.