1

I have a simple php contact form i got from a web tutorial. It worked yesterday, but will not work today. I'd love some help, as I don;t know much php.

php:

<?php
//If the form is submitted
if(isset($_POST['submit'])) {

  //Check to make sure that the name field is not empty
  if(trim($_POST['contactname']) == '') {
    $hasError = true;
  } else {
    $name = trim($_POST['contactname']);
  }


  //Check to make sure that the subject field is not empty
  if(trim($_POST['subject']) == '') {
    $hasError = true;
  } else {
    $subject = trim($_POST['subject']);
  }

  //Check to make sure sure that a valid email address is submitted
  if(trim($_POST['email']) == '')  {
    $hasError = true;
  } else if (!filter_var( trim($_POST['email'], FILTER_VALIDATE_EMAIL ))) {
    $hasError = true;
  } else {
    $email = trim($_POST['email']);
  }

  //Check to make sure comments were entered
  if(trim($_POST['message']) == '') {
    $hasError = true;
  } else {
    if(function_exists('stripslashes')) {
      $comments = stripslashes(trim($_POST['message']));
    } else {
      $comments = trim($_POST['message']);
    }
  }

  //If there is no error, send the email
  if(!isset($hasError)) {
    $emailTo = 'person@domain.com'; // Put your own email address here
    $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
    $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

    mail($emailTo, $subject, $body, $headers);
    $emailSent = true;
  }
}
?>

HTML:

<form role="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
                <?php if(isset($hasError)) { //If errors are found ?>
                  <p class="alert alert-danger">Please check if you've filled all the fields with valid information and try again. Thank you.</p>
                <?php } ?>

                <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
                  <div class="alert alert-success">
                    <p><strong>Message Successfully Sent!</strong></p>
                    <p>Thank you for using our contact form, <strong><?php echo $name;?></strong>! Your email was successfully sent and we&rsquo;ll be in touch with you soon.</p>
                  </div>
                <?php } ?>

                <div class="form-group">
                  <label for="name">Your Name<span class="help-required">*</span></label>
                  <input type="text" name="contactname" id="contactname" value="" class="form-control required" role="input" aria-required="true" />
                </div>

                <div class="form-group">
                  <label for="email">Your Email<span class="help-required">*</span></label>
                  <input type="text" name="email" id="email" value="" class="form-control required email" role="input" aria-required="true" />
                </div>

                <div class="form-group">
                  <label for="subject">Subject<span class="help-required">*</span></label>
                  <input type="text" name="email" id="subject" class="form-control required" role="input" aria-required="true">
                </div>

                <div class="form-group">
                  <label for="message">Message<span class="help-required">*</span></label>
                  <textarea rows="8" name="message" id="message" class="form-control required" role="textbox" aria-required="true"></textarea>
                </div>

                <div class="actions">
                  <input type="submit" value="Send Your Message" name="submit" id="submitButton" class="btn btn-grey" title="Click here to submit your message!" />
                  <input type="reset" value="Clear Form" class="btn btn-grey pull-right" title="Remove all the data from the form." />
                </div>
            </form>

It gets hung up on the validation. Not sure why.

eloist
  • 465
  • 3
  • 10
  • 22
  • 3
    "It gets hung up on the validation". What does that mean? You need to do a better job of describing the problem. You also need to let us know what you've done to troubleshoot this. – John Conde Apr 22 '14 at 15:58
  • When I submit, the page reloads, and gives me the error message: "Please check if you've filled all the fields with valid information and try again. Thank you." As for troubleshooting, I don't really know how to troubleshoot it. I re-read the code several times, but I dont know why this is happening – eloist Apr 22 '14 at 16:02
  • @eloist A question came up today about the use of `if(function_exists('stripslashes'))` with a link to your question. Is there a special reason as to why you're using that? Or, was it part of that tutorial you stated that you followed? – Funk Forty Niner Nov 04 '15 at 18:48
  • @Fred-ii- It's been soooo long since this thread happened. I'm sure that if statement was part of the tutorial I used, as I had no clue how to write php back then. Best of luck. – eloist Nov 09 '15 at 15:13
  • @eloist Yes I know it's been a while. Just saying that this question appeared in Google's results while searching for a similar code. – Funk Forty Niner Nov 09 '15 at 18:12
  • @Fred-ii- gotcha. Like I said, this statement was included in the tutorial. That's the reason I included it. – eloist Nov 09 '15 at 21:36
  • @eloist Thanks, *cheers* – Funk Forty Niner Nov 09 '15 at 21:45

1 Answers1

1

$_POST["subject] is not defined in your form. Your SUBJECT field is called EMAIL:

Change:

<div class="form-group">
  <label for="subject">Subject<span class="help-required">*</span></label>
  <input type="text" name="email" id="subject" class="form-control required" role="input" aria-required="true">
</div>

With:

<div class="form-group">
  <label for="subject">Subject<span class="help-required">*</span></label>
  <input type="text" name="subject" id="subject" class="form-control required" role="input" aria-required="true">
</div>
Teknotica
  • 1,096
  • 6
  • 19