0

I am trying to create a contact form using php and a html form. When the user clicks submit I want the details that they have entered into the form to be sent in an email.

When the user clicks submit they are shown a message to say the message has been sent but when I check my email nothing has been sent.

Here is my code, please note the email has been changed to show the code here.

<div id = "form">

<form action ="contact2.php" method="post">
Name:
<input type="text" name="name">
<br>
<br>
Email:
<input type="text" name="email">
<br>
<br>
Message:
<br>
<br>
<TEXTAREA NAME="message" ROWS=6 COLS=40>
</TEXTAREA>

<br>
<br>

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

</div>

</form>

<?php

$field_name = $_POST['name'];
$email = $_POST['email'];
$field_message = $_POST['message'];

$mail_to = 'example@yahoo.co.uk';

$subject = 'Message from a site visitor ' . $field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$email."\n";
$body_message .= 'Message: '.$field_message;

$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
<script language="javascript" type="text/javascript">

alert('Thank you for the message. We will contact you shortly.');

window.location = 'contact.php';
</script>

else { ?>
<script language="javascript" type="text/javascript">

alert('Message failed. Please, send an email to example@yahoo.co.uk');

window.location = 'contact.php';
</script>
<?php
}?>
Rebekah
  • 77
  • 1
  • 2
  • 11

1 Answers1

-1

you want to have all that javascript wrapped in a php echo or else it doesnt work.

so...

if ($mail_status) {
echo '<script language="javascript" type="text/javascript">'; 

thats just a bit of the code you need to implement... you want to echo every javascript line too.

Max Alexander Hanna
  • 3,388
  • 1
  • 25
  • 35