2011-06-22 58 views
-1

我正在处理联系表单。现在,当用户想要重新输入不起作用的密码时,我遇到了问题。我知道这很可能是一些小事,但我还没有弄明白。这里是我的代码:Php联系表格

<?php 
function showForm($strMessage){ 
echo "<h1>".$strMessage."</h1>"; 
echo " <p>Note: fields marked with '*' are required</p>\n"; 
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">\n"; 
echo "<table width=\"45%\" class=\"formtable\" cellpadding=\"3\" cellspacing=\"0\">\n"; 
echo " <tr>\n"; 
echo "  <td><span id=\"rfvname\">* Name:</span></td>\n"; 
echo "  <td><input type=\"text\" name=\"name\" value=\"".$_POST['name']."\" /></td>\n"; 
echo " </tr>\n"; 
echo " <tr>\n"; 
echo "  <td><span id=\"rfvemail\">* E-mail:</span></td>\n"; 
echo "  <td><input type=\"text\" name=\"email\" value=\"".$_POST['emial']."\" /></td>\n"; 
echo " </tr>\n"; 
echo "  <tr>\n"; 
echo "  <td><span id=\"rfvusername\">* Username:</span></td>\n"; 
echo "  <td><input type=\"text\" name=\"username\" value=\"".$_POST['username']."\" /></td>\n"; 
echo " </tr>\n"; 
echo " <tr>\n"; 
echo "  <td><span id=\"rfvpword\">* Password:</span></td>\n"; 
echo "  <td><input type=\"password\" name=\"pword\" value=\"".$_POST['pword']."\" /><br /><span style=\"font-size:9px;\"><em>(at least 4 chars) </em></span></td>\n"; 
echo " </tr>\n"; 
echo " <tr>\n"; 
echo "  <td><span id=\"rfvpword\">* Re-enter Password:</span></td>\n"; 
echo "  <td><input type=\"text\" name=\"pword\" value=\"".$_POST['pword']."\" /></td>\n"; 
echo " </tr>\n"; 
echo " <tr>\n"; 
echo "   <td>&nbsp;</td>\n"; 
echo "   <td><input type=\"submit\" value=\"Submit\" class=\"btnSubmit\" id=\"btnSubmit\" name=\"submit\" /></td>\n"; 
echo " </tr>\n"; 
echo "</table>\n"; 
echo "</form>\n"; 
    } 
    ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<title>Contact Form</title> 
<style type="text/css"> 
body{ 
      background-color:#FFBD40; 
      color:#000000; 
      font-size:100%; 
      font-family:Georgia,Verdana,"Times New Roman",sans-serif; 
     } 


#container{ 
      background:#FFF573; 
      width:800px; 
      margin:auto; 
      padding:5px 10px 5px 10px; 
      border:6px double #000000; 
     } 
</style> 
    </head> 
    <body> 
    <div id="container"> 
    <?php 
if (isset($_POST['submit'])){ 
    if (trim($_POST['name'])==""){ 
     $strMessage="Please enter your name!"; 
     showForm($strMessage); 
    } 
    elseif (strlen(trim($_POST['pword']))<=3){ 
     $strMessage="Your password must be at least 4 characters long!"; 
     showForm($strMessage); 
    } 
    else{ 
     $strMessage="Thank you, your information has been submitted. Below is the information you sent:"; 
     $strMessageBody.="Name: ".trim(stripslashes($_POST['name']))."<br />"; 
     $strMessageBody.="E-mail: ".trim(stripslashes($_POST['email']))."<br />"; 
     $strMessageBody.="UserName: ".trim(stripslashes($_POST['username']))."<br />"; 
     $strMessageBody.="Password: ".trim(stripslashes($_POST['pword']))."<br />"; 
     echo "<h1>".$strMessage."</h1>"; 
     echo $strMessageBody; 
    } 
     } 
    else{ 
    $strMessage= "Please fill out the form below to send your information:"; 
    showForm($strMessage); 
    } 
     ?> 
    </div> 
     </body> 
     </html> 
+3

所以,不工作意味着......在这种情况下究竟是什么?你得到什么而不是你所期待的? – thescientist

+3

我不认为这两个密码字段应该有相同的名称“pword” –

+0

需要更多的问题描述。什么不工作,你想如何工作?一般来说,我不会再为他们填写密码字段。如果用户希望浏览器保存密码,他们可以,但不要将其放回$ _POST变量的密码字段中。此外,对于该输入类型使用type =“passwword”,而不是文本。 – kinakuta

回答

3

如果2个密码请求比较起见,你应该给他们不同的名字;否则第二个输入将覆盖第一个输入,而您只能得到1个值。

这里是你如何去验证你的密码:

if (trim($_POST['name'])==""){ 
    $strMessage="Please enter your name!"; 
    showForm($strMessage); 
} 
/* START ADD */ 
elseif ($_POST['pword1'] != $_POST['pword2']) { 
    $_POST['pword1'] = NULL; // Reset the values of pword1 so it is not in the form 
    $_POST['pword2'] = NULL; // Reset the values of pword2 so it is not in the form 
    $strMessage="Passwords do not match!"; 
    showForm($strMessage); 
} 
/* END ADD */ 
elseif (strlen(trim($_POST['pword']))<=3){ 
    $strMessage="Your password must be at least 4 characters long!"; 
    showForm($strMessage); 
} 
else ... 
+0

好的,如果用户没有相同的密码,我需要弹出一些东西,请输入相同的密码,我将如何编码? – andy

+0

即时通讯有点困惑GiladG – andy

+0

我有点困惑如何做到这一点会像全球$错误; ($错误为$ err){ech $ err,“
”; }但我不完全理解它。 – andy

1

,您可以给“重新输入密码”字段,以其他名称,如

echo "  <td><span id=\"rfvpword\">* Re-enter Password:</span></td>\n"; 
echo "  <td><input type=\"text\" name=\"repword\" value=\"".$_POST['repword']."\" /></td>\n"; 

然后在验证部分放在另外一个elseif来检查它是否匹配密码

elseif ($_POST['repword'] != $_POST['pword']){ 
    $strMessage="Re-type password must be same as password!"; 
    showForm($strMessage); 
} 

未测试代码,只是一个大致的想法,希望它可以帮助你