2012-11-02 64 views
-1

我有一個php聯繫表單,它的驗證等都可以正常工作,但我有一個小問題。我調整過的原始代碼將頁面重定向到一個新的提交的感謝消息,我不滿意,所以我設法得到一個感謝消息顯示在原始頁面上,但輸入表單內容仍然停留,我寧願它沒有。更好的是,我希望能夠完全隱藏表單並用謝謝替換它。提交時隱藏或清除一個PHP表格

我應該提到,當它完成後,它將被放置在一個頁面上與其他項目,所以它只是我隱藏或清除後的形式。

這是頭

<?php 
$your_email ='[email protected]';// <<=== update to your email address 

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> 

產生的表格可以在這裏找到>>http://dev.craigomatic.co.uk/captcha2/

我已經嘗試了一些方法,我發現通過搜索,但部分由於我的基本知識的PHP,部分原因是這些解決方案似乎並沒有像這樣一起工作。

任何幫助將不勝感激。我目前的形式被無情地垃圾郵件,所以我真的很想讓這個版本正常運行。

回答

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秒內。

+0

,似乎工作一種享受股利。謝謝。我並不太在乎跟蹤IP地址。這部分附帶了我用於驗證碼的代碼。我真的留下了它的新奇價值,因爲似乎沒有必要刪除它。 – UntitledGraphic

1
if(empty($_POST)){ 
//You form goes here; 
} 
else{ 
echo $thankyou; 
} 

你也可以有類似:

if(empty($thankyou)){ 
//You form goes here; 
} 
else{ 
echo $thankyou; 
} 
0

你最好創建單獨的操作文件,並顯示由於信息的使用:

if(isset($_SESSION['msg'])){ 
echo $_SESSION['msg']; 
} 
else{ 
//display from here 
} 
0

可以觸發這個功能的onsubmitonclick表格上的事件

<script> 
    function hideform() 
    { 
    document.forms['contact_form'].style.visibility = 'hidden'; 
    } 
    </script> 
0

您可以使用jQuery清除成功提交的表單div。

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

如果myForm的和形式的容器是表單的ID和包含表單