【问题标题】:Extend PHP form with required fields and validation使用必填字段和验证扩展 PHP 表单
【发布时间】:2016-04-15 13:49:49
【问题描述】:

我需要帮助来扩展我的 PHP 脚本。我需要必填字段并验证注册表单中的电子邮件字段。

这是我的代码,我尝试使用不同的 if 和 echo 代码,但其中没有任何作用。

<?php

$email = $_POST['email'];
$from = $email;

$to = "xy@gmail.com";
$subject = "SIGN UP";

$emailbody = "";
$emailbody .= "Email: " . $email . "\n";

$header = "";
$header .= "From:" . $from . " \n";
$header .= "Content-Type:text/plain;charset=utf-8";

// Sending message
$success = mail($to, $subject, $emailbody, $header);

if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=confirmation_signup.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
?>

代码有效,电子邮件已送达,但如果未填写字段,则表单不应该成功。提前谢谢您。

【问题讨论】:

  • 我的回答对你有帮助吗?

标签: php forms validation required-field


【解决方案1】:

您可以检查电子邮件字段是否为空,然后您可以使用filter_var 来验证提供的电子邮件。

这是一个例子

<?php
if(!empty($_POST['email']) && (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) )
{
    $email = $_POST['email'];
    $from = $email;

    $to = "xy@gmail.com";
    $subject = "SIGN UP";

    $emailbody = "";
    $emailbody .= "Email: " . $email . "\n";

    $header = "";
    $header .= "From:" . $from . " \n";
    $header .= "Content-Type:text/plain;charset=utf-8";

    // Sending message
    $success = mail($to, $subject, $emailbody, $header);

    if ($success)
    {
        print "<meta http-equiv=\"refresh\" content=\"0;URL=confirmation_signup.html\">";
    }
    else
    {
    print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
    }
}
else print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
?>

【讨论】:

  • 这对我有用!谢谢!但我还有另一个问题——也许你也有解决办法……
  • @DietrichMöller 这是什么?
【解决方案2】:

看来,您从 POST 中收集的唯一信息是电子邮件,因此请先检查是否是 empty,然后是 validate it

<?php

if(!empty($_POST['email']) && (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false)){

  $email = $_POST['email'];
  $from = $email;

  $to = "xy@gmail.com";
  $subject = "SIGN UP";

  $emailbody = "";
  $emailbody .= "Email: " . $email . "\n";

  $header = "";
  $header .= "From:" . $from . " \n";
  $header .= "Content-Type:text/plain;charset=utf-8";

  // Sending message
  $success = mail($to, $subject, $emailbody, $header);

  if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=confirmation_signup.html\">";
  }
  else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
  }
}
else echo "email required";
?>

在它到达服务器之前,您可以通过首先进行一些客户端验证来帮助您的用户,以确保 input is an emailis required

【讨论】:

    【解决方案3】:

    问题已解决,但出现了另一个问题。 html 表单无法正确发送邮件。我尝试了六个邮件地址。如果有人提交表格,邮件有时会发送到其中一个 - 有时会发送到另一个 - 有时会发送到没有地址。我听说不要使用常规的 mail() 命令。谁能告诉我这个?

    <?php
    if(!empty($_POST['email']) && !empty($_POST['name']) && !empty($_POST['message']) && (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false)){
    
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = $email;
    $to = "test@test.com";
    $subject = "SUPPORT";
    
    $emailbody = "";
    $emailbody .= "Full name: " . $name . "\n";
    $emailbody .= "Email: " . $email . "\n";
    $emailbody .= "Message: " . $message . "\n";
    
    $header = "";
    $header .= "From:" . $from . " \n";
    $header .= "Content-Type:text/plain;charset=utf-8";
    
    $success = mail($to, $subject, $emailbody, $header);
    
    if ($success){
    print "<meta http-equiv=\"refresh\" content=\"0;URL=de/confirmation.html\">";
    }
    else{
    print "<meta http-equiv=\"refresh\" content=\"0;URL=de/error.html\">";
    }
    }
    else print "<meta http-equiv=\"refresh\" content=\"0;URL=de/error.html\">";
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 2022-11-22
      • 1970-01-01
      • 2023-04-08
      相关资源
      最近更新 更多