我有一个 php 联系表单,它在验证等方面都能很好地工作,但我有一个小问题。我调整的原始代码将页面重定向到一个新页面,并在提交时收到一条我不满意的感谢消息,因此我设法在原始页面上显示了一条感谢消息,但是输入表单内容仍然存在,我宁愿它没有。更好的是,我希望能够完全隐藏表单并用谢谢替换它。
我应该提一下,完成后它会与其他项目一起放在一个页面上,所以它只是我隐藏或清除之后的表单。
这是标题中的代码
<?php
$your_email ='email@example.com';
session_start();
$errors = '';
$name = '';
$company = '';
$visitor_email = '';
$phone = '';
$user_message = '';
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$company = $_POST['company'];
$visitor_email = $_POST['email'];
$phone = $_POST['phone'];
$user_message = $_POST['message'];
///------------Do Validations-------------
if(empty($name)||empty($visitor_email))
{
$errors .= "\n Name and Email are required fields. ";
}
if(IsInjected($visitor_email))
{
$errors .= "\n Bad email value!";
}
if(empty($_SESSION['6_letters_code'] ) ||
strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
{
//Note: the captcha code is compared case insensitively.
//if you want case sensitive match, update the check above to
// strcmp()
$errors .= "\n The captcha code does not match!";
}
if(empty($errors))
{
//send the email
$to = $your_email;
$subject="New form submission";
$from = $your_email;
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$body = "A user $name submitted the contact form:\n".
"Name: $name\n".
"Company: $company\n".
"Email: $visitor_email \n".
"Phone: $phone\n".
"Message: \n ".
"$user_message\n".
"IP: $ip\n";
$headers = "From: $from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to, $subject, $body,$headers);
//header('Location: #thanks');
$myForm = '< style="visibility: hidden;">';
$thankyou = file_get_contents("thank-you.html");
}
}
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
这是表单本身减去一大块我认为与问题无关的javascript验证
<?php
if(!empty($errors)){
echo "<p class='err'>".nl2br($errors)."</p>";
}
?>
<div id="footer">
<div class="twelve-column-wrapper">
<div class="six-column-wrapper">
<div class="six-column">
<h3>Why not get in touch</h3>
</div>
<div id='contact_form_errorloc' class='err'></div>
<form method="POST" name="contact_form"
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<div class="three-column">
<p>
<label for='name'>Name: </label>
<br>
<input type="text" name="name" value='<?php echo htmlentities($name) ?>'>
</p>
</div>
<div class="three-column">
<p>
<label for='company'>Your Company: </label>
<input type="text" name="company" id="company" value='<?php echo htmlentities($company) ?>'/>
</p>
</div>
<div class="three-column">
<p>
<label for='email'>Email: </label>
<br>
<input type="text" name="email" value='<?php echo htmlentities($visitor_email) ?>'>
</p>
</div>
<div class="three-column">
<p>
<label for='phone'>Phone No. </label>
<input type="text" name="phone" id="phone" value='<?php echo htmlentities($phone) ?>'/>
</p>
</div>
<div class="six-column">
<p>
<label for='message'>Message:</label>
<br>
<textarea name="message" rows=8 cols=30><?php echo htmlentities($user_message) ?></textarea>
</p>
</div>
<div class="three-column">
<p> <img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br>
<label for='message'>Enter the code above here :</label>
<br>
<input id="6_letters_code" name="6_letters_code" type="text">
<br>
<small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small> </p>
<input id="submit" type="submit" value="Submit" name='submit'>
</div>
<div class="six-column"> <?php echo $thankyou; ?> </div>
</form>
</div>
</div>
</div>
我尝试了一些通过搜索找到的方法,但主要是由于我对 PHP 的基本了解而失败了。
任何帮助将非常感激。