-2

私は、検証などですべてうまく機能するphpコンタクトフォームを持っていますが、それには1つの問題があります。私が微調整した元のコードは、私が不満だった送信時に感謝のメッセージでページを新しいページにリダイレクトしたので、元のページに表示する感謝のメッセージを取得することができましたが、入力フォームのコンテンツまだ残っています。さらに良いことに、フォームを完全に非表示にして、ありがとうに置き換えたいと思います。

完成すると他のアイテムと一緒にページに配置されるので、非表示またはクリアした後のフォームです。

これはヘッダーのコードです

<?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 を追跡しなければならないためにスパムにされたくない場合は、電子メールを送信する前に、たとえば過去 30 秒間に IP が電子メールを送信していないことを確認してください。

于 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

この関数は、フォームの送信またはクリックイベントでトリガーできます

 <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 に答える