使用必填字段和验证扩展PHP表单

时间:2016-04-15 13:49:50

标签: php forms validation required-field

我需要帮助扩展我的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\">";
}
?>

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

3 个答案:

答案 0 :(得分:0)

看来电子邮件是您从POST收集的唯一内容,因此请先检查empty是否validate itinput is an email

<?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";
?>

在它到达服务器之前,您可以先通过一些客户端验证来帮助您的用户,以确保is requiredimport as

答案 1 :(得分:0)

您可以检查电子邮件字段是否为空,然后您可以使用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\">";
?>

答案 2 :(得分:0)

问题已解决,但出现了另一个问题。 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\">";
?>