2016-03-30 228 views
0

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

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

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

HTML代码:

<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>'; 
?> 
+1

在提交之前使用javascript来限定用户输入。 – Martin

回答

0

,因为它看起来像你张贴到一个比你对形式的不同PHP文件,你有两个选择:

  1. 重定向回表单页面提交错误
  2. 显示错误并暂停执行页面

第一个选项是更好的选项(作为对页面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; 
} 

值现在,有很多方法,传回错误消息的原始页面,但最简单的例子只是增加了一个GET参数的URL。

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

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

+0

非常感谢你,我真的很感激。有没有一种方法可以在不继续提交表单并随后丢失输入的情况下显示错误消息? – Billy

+0

@比利如果它的工作,请接受这个答案 –

+0

@比利如果你的表单是在你的php代码所在的同一个文件,那么你可以使用submited的输入,只是重新回显他们到value =“”,否则你可以使用会话,并将表单数据保存到会话中,并在用户返回到表单页面时,从会话中获取输入数据,然后从会话中清除这些输入。 –