我正在尝试构建一个表单并遇到了一些困难.如果存在验证错误,则在发布表单时,将删除所有输入的数据!有没有办法在表格中保留日期?我也试图将表单数据发送到名为contact-engine.PHP的页面
我可以通过将表单操作更改为action =“contact-engine.PHP”来发送它,但之后我的验证不起作用!经过几个小时的努力来解决这个问题后,我基本上陷入困境并且已经没有想法了.代码如下:
<?PHP
// define variables and set to empty values
$addDateErr = $nameErr = $emailErr = $subjectErr = $messageErr = $questionErr = "";
$addDate = $name = $email = $subject = $message = $question = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
//Date
if (empty($_POST["addDate"]))
{$comment = "";}
else
{$comment = test_input($_POST["addDate"]);}
//Name
if (empty($_POST["name"]))
{$nameErr = "Name is required";}
else
{
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$nameErr = "Only letters and white space allowed";
}
}
//Email
if (empty($_POST["email"]))
{$emailErr = "Email is required";}
else
{
$email = test_input($_POST["email"]);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "Invalid email format";
}
}
//Subject
if (empty($_POST["subject"]))
{$comment = "";}
else
{$comment = test_input($_POST["subject"]);}
//Message
if (empty($_POST["message"]))
{$messageErr = "A message is required";}
else
{$comment = test_input($_POST["message"]);}
//Question
if (empty($_POST["question"]))
{$comment = "";}
else
{$comment = test_input($_POST["question"]);}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" id="form" action="
<?PHP echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p><span class="error">* required field.</span></p><br />
<!--Date--><div class="contact-font">
Date Photography needed (if necessary)<br />
<input type="text" name="addDate" id="datepicker" size="25" value="
<?PHP echo $addDate;?>">
</div>
<!--Name--><div class="contact-font" style=" margin-top: 20px;">
<span class="asterix">* </span>Name:<br />
<input type="text" name="name" class="border" size="25"><span class="error">
<?PHP echo $nameErr;?></span>
</div>
<!--Email--><div class="contact-font" style=" margin-top: 20px;">
<span class="asterix">* </span>Email:<br />
<input type="text" name="email" class="border" size="25">
<span class="error"><?PHP echo $emailErr;?></span>
</div>
<!--Subject--><div class="contact-font" style=" margin-top: 20px;">
Subject:<br />
<input type="text" name="subject" class="border" size="25"<?PHP echo $subject;?>">
</div>
<!--Message--><div class="contact-font" style=" margin-top: 20px;">
<span class="asterix">* </span>Message:<br />
<textarea cols="40" rows="10" name="message" class="border"></textarea>
<span class="error"><?PHP echo $messageErr;?></span>
</div><br />
<p id="info-req">How did you find about Darren Morton Photography?</p>
<!--Question--><select id="marketing" name="question"<?PHP echo $question;?>">
<option value="----------" style="color:black">----------</option>
<option value="Web Search" style="color:black">Google Search</option>
<option value="Web Search" style="color:black">Social Media</option>
<option value="Word of mouth" style="color:black">Word of mouth</option>
<option value="Other" style="color:black">Other</option>
</select><br /><br />
<div>
<input type="submit" value="Send" id="submit">
</div>
</form>
解决方法:
你应该可以做这样的事情.保留from action之前没有你的contact-engine.PHP文件.然后看看是否设置了$_POST,然后它将使用最近的值填充输入.例
<input type="text" name="name" class="border" size="25" value="<?PHP if(isset($_POST['name'])) { echo $_POST['name']; } ?>"><span class="error">
<?PHP echo $nameErr;?></span>
然后你可以为你的其他输入做到这一点.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。