-2

我有一个 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 的基本了解而失败了。

任何帮助将非常感激。

4

5 回答 5

1
<?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>
<?php
if(!isset($_POST['submit'])):
?>
            <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>

            </form>
<?php
endif;
?>
            <div class="six-column"> <?php echo $thankyou; ?> </div>
        </div>
    </div>
</div>

这将隐藏表单。但这不会阻止您收到垃圾邮件。例如,如果您不想被垃圾邮件发送到必须跟踪 IP,并且在提交电子邮件之前检查 IP 是否尚未发送电子邮件,比如说在最后 30 秒内。

于 2012-11-02T09:17:29.347 回答
1
if(empty($_POST)){
//You form goes here;
}
else{
echo $thankyou;
}

你也可以有类似的东西:

if(empty($thankyou)){
//You form goes here;
}
else{
echo $thankyou;
}
于 2012-11-02T09:17:43.027 回答
0

您最好创建单独的操作文件并用于显示感谢消息使用:

if(isset($_SESSION['msg'])){
echo $_SESSION['msg'];
}
else{
//display from here
}
于 2012-11-02T09:25:41.393 回答
0

您可以在表单上触发此功能onsubmitonclick事件

 <script>
    function hideform()
    {
    document.forms['contact_form'].style.visibility = 'hidden';
    }
    </script>
于 2012-11-02T09:26:34.433 回答
0

您可以使用 jQuery 在成功提交时清除表单 div。

$('#myform').submit(function(){
    $('#formcontainer').empty;
});

其中 myform 和 form container 是表单的 id 和包含表单的 div

于 2012-11-02T09:29:46.080 回答