【问题标题】:PHP contact form will not submitPHP 联系表单不会提交
【发布时间】:2014-04-22 15:57:14
【问题描述】:

我有一个从网络教程中获得的简单 php 联系表。它昨天工作,但今天不会工作。我很想得到一些帮助,因为我不太了解 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>

它挂断了验证。不知道为什么。

【问题讨论】:

  • “它挂断了验证”。这意味着什么?您需要更好地描述问题。您还需要告诉我们您为解决此问题所做的工作。
  • 当我提交时,页面重新加载,并给我错误消息:“请检查您是否填写了所有有效信息并重试。谢谢。”至于故障排除,我真的不知道如何排除故障。我重读了几次代码,但我不知道为什么会这样
  • @eloist 今天出现了一个关于使用if(function_exists('stripslashes')) 的问题,并附有指向您问题的链接。你为什么使用它有什么特殊原因吗?或者,它是您说您遵循的教程的一部分吗?
  • @Fred-ii- 自从这个线程发生以来已经很久了。我确定 if 语句是我使用的教程的一部分,因为当时我不知道如何编写 php。祝你好运。
  • @eloist 是的,我知道已经有一段时间了。只是说这个问题在搜索类似代码时出现在谷歌的结果中。

标签: php html contact-form


【解决方案1】:

$_POST["subject] 未在您的表单中定义。您的 SUBJECT 字段名为 EMAIL:

变化:

<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="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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 2017-06-06
    • 2012-04-28
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多