1

I am making a feedback form for my website using php, I thought that it was working, but then it took an arrow to the code.

Anyways, the form has 4 fields: "name", "email", "subject", and "message".

I get the "Message failed" alert if all four fields have content.

Seeing as that is the case, I tested with content in 3 of the 4 fields, which gives me 4 combinations:

combo 1 - "name", "email", "subject"
combo 2 - "name", "email", "message"
combo 3 - "name", "subject", "message"
combo 4 - "email", "subject", "message"

The results were as follows:

combo 1 - "Thank you for your message."
combo 2 - "Thank you for your message."
combo 3 - "Message failed."
combo 4 - "Thank you for your message."

Here is the html code I am using:

<form action="contact.php" method="post">
  <input type="hidden" name="page" value="contact" />
  <input type="hidden" name="req" value="submit" />
  Your Name: <input type="text" name="name" />
  <br/>
  Your Email: <input type="text" name="email" />
  <br/>
  Subject: <input type="text" name="subject" size="69" />
  <br/>
  Message:
  <br/>
  <textarea cols="63" rows="8" name="message"></textarea>
  </br>
  <input type="submit" value="Send" />
  <input type="reset" value="Clear" />
</form>

And here is contact.php:

<?php
  $field_name = $_POST['name'];
  $field_email = $_POST['email'];
  $field_subject = $_POST['subject'];
  $field_message = $_POST['message'];

  $mail_to = 'email@my_website.com';
  $subject = 'Message from a site visitor '.$field_name;

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

  $headers = 'From: '.$field_email."\r\n";
  $headers .= 'Reply-To: '.$field_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 your message.');
  window.location = './contact.html';
</script>
<?php
  }
  else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed.');
window.location = './contact.html';
</script>
<?php
  }
?>

Is there anything wrong with the code? I don't understand why it is doing this.

3
  • 1
    When testing the combinations, did you enter the same value in the fields all the times? I.e. [email protected] in email every time. Commented Mar 1, 2012 at 23:32
  • Well, email was a bad example since it was the only one not used in the failed message. But you get the point. Commented Mar 1, 2012 at 23:34
  • yeah i did put the same value for each field Commented Mar 1, 2012 at 23:40

2 Answers 2

3

Great answer from Anton but I wanted to dig a little bit more into this and expand his answer.

If you want to make sure someone sent the email, require it hardcoded with php (and html5 would be cool too, but this can be avoid so you still need php). I also deleted the javascript bit as it felt irrelevant there. This is what I would try and since it's short I'd do it all in the same page. There are many subtle changes:

<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];


//If all required fields are filled
if (!empty($field_name)&&!empty($field_email)&&!empty($field_message))
  {
  $mail_to = 'email@my_website.com';
  $subject = 'Message from a site visitor: '.$field_name;

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

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

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

  echo "Thank you for your message.";
  }

//If not all required fields are filled display form again.
else
  { ?>
  Please fill all the required fields.<br>

  <form action="" method="post">
  <input type="hidden" name="page" value="contact" />
  <input type="hidden" name="req" value="submit" />

  <?php //The php bits are to retrieve the valid fields ?>
  Your Name*: <input required type="text" value="<?php echo $field_name; ?>" name="name"/>
  <br/>
  Your Email*: <input required type="email" value="<?php echo $field_email; ?>" name="email" />
  <br/>
  Subject: <input type="text" name="subject" value="<?php echo $field_subject; ?>" size="69" />
  <br/>
  Message*:
  <br/>
  <textarea required cols="63" rows="8" name="message"><?php echo $field_message; ?></textarea>
  </br>
  <input type="submit" value="Send" />
  <input type="reset" value="Clear" />
  </form>

  <?php
  }
?>

Otherwise, if you don't care about who sends it, you could just put something like this

$field_name = $_POST['name'];
$field_email = $_POST['email'];
if (empty($field_email)) $field_email="[email protected]";
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];

Last thing, I hope this is just a pseudo code as it doesn't comply almost any html rule. You need < html > tags, < body > etc...

Just tested and worked great (;

Sign up to request clarification or add additional context in comments.

1 Comment

I still have the same problem. I tried the 4 combinations and had the same issues, although this time it says "Thank you for your message." but I don't receive the email.
1

The problem is probably this:

Quote from http://php.net/manual/en/function.mail.php

When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.

Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. The From header sets also Return-Path under Windows.

In other words, if you do not have a header containing a proper "From: [email protected]" an error will occur.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.