问题描述
|
好的,我正在尝试获取此表单,如果有人跳过表单上的内容,它将显示出来并告诉他们。我是新手,如果有人不输入信息,我不会理解如何出错。这就是我所拥有的
<?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=\\\"repword\\\" value=\\\"\".$_POST[\'repword\'].\"\\\" /></td>\\n\";
echo \" </tr>\\n\";
echo \" <tr>\\n\";
echo \" <td> </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 />\";
$strMessageBody.=\"Re-enter Password: \".trim(stripslashes($_POST[\'repword\'])).\"<br />\";
echo \"<h1>\".$strMessage.\"</h1>\";
echo $strMessageBody;
}
}
else{
$strMessage= \"Please fill out the form below to send your information:\";
showForm($strMessage);
}
?>PHP
$$errors = array();
if (isset($_REQUEST[\"seen_already\"])){
validate_data();
if(count($errors) ! = 0){
display_errors();
display_welcome();
} else {
display_welsome();
}
function validate_data()
{
global $errors;
if($_REQUEST[\"Name\"]==\"\"){
$errors[] = \"<FONT COLOR=\'RED\'>Please enter your first name</FONT>\";
}
if($_REQUEST[\"E-mail\"]==\"\"){
$error[] = \"<FONT COLOR=\'RED\'>Please enter your E-Mail>/FONT>\";
}
if($_REQUEST[\"UserName\"]==\"\"){
$errors[] = \"<FONT COLOR=\'RED\'>Please enter your Username</FONT>\";
}
if($_REQUEST[\"Password\"]==\"\"){
$errors[] = \"<FONT COLOR=\'RED\'>Please enter your Password</FONT>\";
}
if($_REQUEST[\"RE-enter Password\"]==\"\"){
$errors[] = \"<FONT COLOR=\'RED\'>Please re-enter your Password</FONT>\";
}
}
function display_errors()
{
global $errors;
foreach($errors as $err){
echo $err,\"<BR>\";
}
}
function process_data()
</div>
</body>
</html>
好吧,echo \'s和\“和\\是我必须全部设置的方式。所以应该看起来像这样。我要问的是,是否有人可以帮助我理解如何执行错误代码。不是为我做这件事,而是要帮助我理解它。我已经开始使用的代码是否可以正常工作或者是我搞砸了?感谢所有拒绝我的人。我只是在问一个简单的问题。
解决方法
您的问题是这样的:
$_REQUEST[\"RE-enter Password\"]
您将文本标签用于表单字段。但是您的实际输入字段称为:
$_REQUEST[\"repword\"]
其他所有领域也是如此。 “ 3”数组根据HTML表单中的“ 4”属性来保存键。
其他:您还可以简化表单输出。您应该使用Heredoc字符串来代替许多ѭ5,以避免许多\"
引号和\\
反斜杠转义:
echo <<<HTML
<h1>$strMessage</h1>
<p>Note: fields marked with \'*\' are required</p>
<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">
<table ...
<td><span id=\"rfvname\">* Name:</span></td>
<td><input type=\"text\" name=\"name\" value=\"$_POST[name]\"></td>
...
HTML;
在此之前还要加上ѭ9(以简化/安全起见)。