【问题标题】:Error with PHP Form - Non-functional [closed]PHP表单错误 - 非功能[关闭]
【发布时间】:2015-10-02 06:58:48
【问题描述】:

试图让这个表单工作。已完整发布代码。

它不起作用,即,如果所有字段都是空白的,它会直接转到php_mailer_form.php 页面中的contactError.html 页面。 (已在底部发布php_mailer_form.php

如果所有字段都填写了,它就可以工作。

如果为空或部分填写,则不会触发任何错误消息。直接去contactError.html

我在这里缺少什么?我已经尝试了很多不同的变体来完成这项工作,但迄今为止都没有。

<html lang="en">
<head>
  <meta charset="utf-8">

  <title> Contact</title>

  <style type="text/css">
      .error {
        color: #FF0000;
      }

  </style>
</head>


<body>

<?php
session_start(); //allows use of session variables

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (!isset($_POST["first-name"])) {
     $firstNameErr = "First name is required";
   } else {
     $first_name = test_input($_POST["first-name"]);
   }

    if (!isset($_POST["last-name"])) {
     $lastNameErr = "Last name is required";
   } else {
     $last_name = test_input($_POST["last-name"]);
   }

   if (!isset($_POST["email"])) {
     $emailErr = "Email is required";
   } else {
     $email = test_input($_POST["email"]);
   }

   if (!isset($_POST["message"])) {
     $messageErr = "Message is required";
   } else {
     $message = test_input($_POST["message"]);
   }

   if(isset($first_name) && isset($last_name) && isset($email) && isset($message))
   {
     $_SESSION['first_name'] = $first_name;
     $_SESSION['last_name'] = $last_name;
     $_SESSION['email'] = $email;
     $_SESSION['message'] = $message;

     header("Location: php_mailer_form.php");
   }
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>

<div class="ui container">
<div class="ui segment">
  <div>

    <div class="ui fluid five item tabular menu">
      <a class="item" href="index.html">Home</a>
      <a class="item" href="about.html">About</a>
      <a class="item" href="rooms.html">Rooms Info & Rates</a>
      <a class="item" href="book.html">To Book</a>
      <a class="item" href="contact.html">Contact</a>
    </div>

  </div>

<div class="ui two column stackable grid">

<div class="ten wide column">
<form class="ui form"  method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  <div class="field">
    <label>First Name</label>
    <input name="first-name" id="first-name" placeholder="First Name" type="text">
     <?php if(isset($firstNameErr)) print ('<span class="error">* ' . $firstNameErr . '</span>'); ?>
  </div>
  <div class="field">
    <label>Last Name</label>
    <input name="last-name" id="last-name" placeholder="Last Name" type="text">
     <?php if(isset($lastNameErr)) print ('<span class="error">* ' . $lastNameErr . '</span>'); ?>
  </div>
    <div class="field">
    <label>Email</label>
    <input name="email" id="email" placeholder="Email" type="email">
     <?php if(isset($emailErr)) print ('<span class="error">* ' . $emailErr . '</span>'); ?>
  </div>
  <div class="field">
    <label>Message</label>
    <textarea rows="2" placeholder="Please type in your message" name="message" id="message"></textarea>
     <?php if(isset($messageErr)) print ('<span class="error">* ' . $messageErr . '</span>'); ?>
  </div>

  <button class="ui button" type="submit">Submit</button>
</form>
  </div>

  <div class="six wide column">
    <br><br>
    <img class="ui centered large bordered rounded image" src="images/tobereplaced.jpg">
  </div>
</div>

</div>

<div class="ui two column grid">
  <div class="ui left aligned ">
    <p>Left Footer Stuff Here</p>
  </div>

  <div class="ui right aligned">
    <p>Right Footer Stuff Here</p>
  </div>

</div>

</div>
</body>
</html>

这里是php_mailer_form.php

<?php

session_start();

$first_name = $_SESSION['first-name'];
$last_name = $_SESSION['last-name'];
$email = $_SESSION['email'];
$message = nl2br($_SESSION['message']);

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'host_specified';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'email_specified';                 // SMTP username
$mail->Password = 'password_specified';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; 

$mail->addReplyTo( $email, $first_name );
$mail->addAddress( $email, $first_name );
$mail->addAddress( 'email_specified', 'Staff' );
$mail->From = 'email_specified';
$mail->FromName = 'Staff';


$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Hotel Room Request';
$mail->Body    = $message; 

$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';

if(!$mail->send()) {
    header('location: url/contactError.html');

} else {
    header('location: url/contactResult.html');

}

【问题讨论】:

  • 出于好奇,您上次问这个问题时是否尝试过我的回答? stackoverflow.com/questions/32853422/…
  • 输出 HTML 后不能调用session_start()。它必须在任何输出之前完成。如果您启用了警告,您会看到警告“标头已发送”。
  • ...我闻起来像复制品 ^
  • @Fred-ii- 我认为你可能是对的......像往常一样!
  • @Rasclatt 或者......也许不是。见下文... V V V V V

标签: php html forms


【解决方案1】:

不要使用isset()来测试是否填写了表单域。如果表单域为空,提交表单时会设置为空字符串,if (!isset($_POST['fieldname']))不会检测到。请改用empty()

if (empty($_POST['first-name'])) {
    $firstNameErr = "First name is required";
} else {
    $first_name = test_input($_POST["first-name"]);
}

【讨论】:

  • 嘿,谢谢!抱歉延迟回复。这个答案有一个轻微的例外。不确定是否是创建另一个问题的好方法。无论如何,如果所有字段都为空,则在点击提交按钮时,会触发警告。所以这是个好消息。但是,当我尝试填写一个字段,然后点击提交按钮时,所有空字段都会发出警告,但对于已填写的字段,它会清除我在那里写的任何内容。我该如何解决?还是我应该将其创建为另一个问题?感谢您的宝贵时间。
  • value="&lt;?php if(isset($first_name)) { echo $first_name; }?&gt;"放在&lt;input&gt;标签里复制之前的值。
  • 你也可以给所有的输入加上required属性,浏览器根本不让他们提交表单。
  • 成功了!谢谢! =) 星期五快乐! (至少在我所在的地区,今天是星期五!)
  • 确认一下,Barmar 所说的 isset 在这里是错误的,这是真的,我之前的错误!因此,您应该将这 4 个测试更改为“空”。将第一个文件中的 PHP 代码块移动到 标记之前,并添加您的 doctype。
猜你喜欢
  • 2013-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-11
  • 2014-12-29
相关资源
最近更新 更多