PHP表单验证 - 简单

时间:2016-03-30 14:35:54

标签: php forms validation

我在PHP方面苦苦挣扎,并想在我的电子邮件字段中添加一些非常简单的验证,以便用户的电子邮件地址必须包含“@”。

请有人帮助我。我知道有很多其他类似的线程,但是,我似乎无法通过使用其他人的线程来纠正我的代码。

我真的很感谢这方面的帮助,但我确实要求如果你能够提供帮助,请尽量不要重构我的代码,因为我希望尽可能简单地使用我现有的代码到位。

HTML code:

<form action="Scripts/studentinfo.php" method="get" id="studentinfo" onsubmit="return checkform()">

<fieldset>
    <legend> Get in Contact </legend>
    <label>
    Email Address: <input type="text" name="email" value="your email address" />
    </label>
    <label>
    What department would you like to contact?
    <select name="subject">
       <option value="Complaint">Complaint</option>
       <option value="Website Issue">Website Issue</option>
       <option value="General Enquiry">General Enquiry</option>
    </select>
    </label>
<br/>
    <label>
    First Name: <input type="text" name="Firstname" value="first name" />
    </label>
    <label>
    Last Name: <input type="text" name="Lastname" value="last name" />
    </label>
    <label>
    Gender
<br/>
        Male: 
        <input type="radio" name="sex" value="male" checked="checked" />
        Female:
        <input type="radio" name="sex" value="female" />
        </label>
        <label>
        <textarea rows="4" cols="15" name="comment" value="please tell us what you think"> </textarea>
        </label>
<input type="submit" name="submit" value="Submit" />

PHP代码:

    <head>
    <link rel="stylesheet" type="text/css" title="Main Stylesheet" href="/stylesheet.css" />
    <link rel="alternate stylesheet" type="text/css" title="Alternative Stylesheet" href="/alternate%20stylesheet.css" />
</head>

<?php
// pull out the values from the request stream sent by the form
// and store those values into memory variables

$email   = $_REQUEST['email'];
$webmail   = $_REQUEST['subject'];
$firstName    = $_REQUEST['Firstname'];
$lastName  = $_REQUEST['Lastname'];
$sex     = $_REQUEST['sex'];
$to = 'email address';
$comment   =$_REQUEST['comment'];
$subject   =$_REQUEST['subject'];


$header    = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"';
$header   .= ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
$htmlhead  = '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">';
$htmlhead .= '<head><title>Feedback Recieved</title>';
$htmlhead .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>';
$htmlbody  = '<body>';

// use the variables that store the information sent from the form.
 mail($to, $subject,"You have received the following feedback:". $comment, "from " . $firstName . " " . $lastName, "From: $email");
$htmlbody .= "<center><h1>Thank You</h1>";
$htmlbody .= "<p><b>" . $firstName . " " .  $lastName ;
$htmlbody .= "</b>, we've recieved an email from your email address: <b>" . $email . "</b>, and will respond as soon as possible!</p></center>";
$htmlbody .= "</body></html>";

// use echo to write all the information out back to the browser
echo $header . $htmlhead . $htmlbody ;
echo '<center>To return, click <a href="/is0502/index.html">here</a>.</center>';
?>

2 个答案:

答案 0 :(得分:0)

由于看起来您发布的文件不同于您在表单上使用的PHP文件,因此您有两种选择:

  1. 重定向回表单页面以显示错误
  2. 显示错误并停止执行页面
  3. 第一个选项是更好的选项(作为页面javascript或HTML5验证的后备)。为此,您必须检查$_POST['email']

    的值
    <?php
    // pull out the values from the request stream sent by the form
    // and store those values into memory variables
    
    $email   = $_REQUEST['email'];
    if (strpos($email,'@') == 0) {
       header("Location: http://[URL OF SOURCE PAGE]?error=bademail"); // replace with correct URL
       exit;
    }
    

    现在,有很多方法可以将错误消息传回原始页面,但最简单的示例只是向URL添加GET参数。

    检查@符号对于电子邮件验证来说有点简单。在@符号前至少需要一个字符,并且在&#39;之前至少需要一个字符。&#39;并且在最后一个&#39;之后至少有两个字母。&#39;对于TLD(例如.com,.co,.video)。使用preg_match而不是strpos,通过正则表达式更容易检查。

答案 1 :(得分:0)

您应该查看此页面php.net,了解有关它们的各种过滤器和信息,以及它们的用途。但是,让我为您发布简单而准确的电子邮件过滤器,该过滤器存在于php中。

<?php
// pull out the values from the request stream sent by the form
// and store those values into memory variables

$email   = $_REQUEST['email'];
$webmail   = $_REQUEST['subject'];
$firstName    = $_REQUEST['Firstname'];
$lastName  = $_REQUEST['Lastname'];
$sex     = $_REQUEST['sex'];
$to = 'email address';
$comment   =$_REQUEST['comment'];
$subject   =$_REQUEST['subject'];

// New code added for email validation
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
  echo("Email is not valid");
  exit();
}

$header    = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"';
$header   .= ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
$htmlhead  = '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">';
$htmlhead .= '<head><title>Feedback Recieved</title>';
$htmlhead .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>';
$htmlbody  = '<body>';

// use the variables that store the information sent from the form.
 mail($to, $subject,"You have received the following feedback:". $comment, "from " . $firstName . " " . $lastName, "From: $email");
$htmlbody .= "<center><h1>Thank You</h1>";
$htmlbody .= "<p><b>" . $firstName . " " .  $lastName ;
$htmlbody .= "</b>, we've recieved an email from your email address: <b>" . $email . "</b>, and will respond as soon as possible!</p></center>";
$htmlbody .= "</body></html>";

// use echo to write all the information out back to the browser
echo $header . $htmlhead . $htmlbody ;
echo '<center>To return, click <a href="/is0502/index.html">here</a>.</center>';
?>

您还可以使用javascript jquery表单验证插件在提交之前验证表单,但用户可以将其覆盖为客户端。

jQuery form validation