2014-02-19 48 views
0

我有一個頁面網站。每個菜單項都會滾動到頁面的不同部分。我有一個HTML和PHP表單,它會重新加載頁面,並在表單未填寫必填字段時顯示錯誤消息。問題是它重新加載到頁面的頂部,而我的表單位於頁面的底部。如何在表單返回錯誤時返回頁面的某個部分

如何獲取表單以將頁面重新加載到聯繫表單所在的部分?當表單已經成功提交時,表單已經完成,但當出現錯誤時表單沒有。

任何幫助將是偉大的。謝謝閱讀。

HTML

<div id="contactBox"> 

      <form name="contact" id="contactForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
       <div id="formLeft"> 
        <p> 
         <label for="name">Name</label> 
         <?php if ($missing && in_array('name', $missing)) { ?> 
         <div class="warningDivLeft"> 
         <span class="warning">Please enter your name</span> 
         </div>        
         <?php } ?> 
         <input type="text" name="name" id="name" tabindex="10" 
         <?php 
         if ($errors || $missing) { 
          echo 'value="' . htmlentities($name, ENT_COMPAT, 'utf-8') . '"'; 
         } 
         ?>  
         > 
        </p> 
        <p>       
         <label for="company">Company Name/Website</label> 
         <input type="text" name="company" id="company" tabindex="30" 
         <?php 
         if ($errors || $missing) { 
          echo 'value="' . htmlentities($company, ENT_COMPAT, 'utf-8') . '"'; 
         } 
         ?>  
         > 
        </p> 
        <p>       
         <label for="email">Email</label> 
         <?php if ($missing && in_array('email', $missing)) { ?> 
         <div class="warningDivLeft"> 
         <span class="warning">Please enter your email</span> 
         </div> 
         <?php } elseif (isset($errors['email'])) { ?> 
         <div class="warningDivLeft"> 
         <span class="warning">Invalid email address</span> 
         </div> 
         <?php } ?> 
         <input type="email" name="email" id="email" tabindex="40" 
         <?php 
         if ($errors || $missing) { 
          echo 'value="' . htmlentities($email, ENT_COMPAT, 'utf-8') . '"'; 
         } 
         ?>  
         > 
        </p> 
        <p>       
         <label for="phone">Phone</label> 
         <input type="text" name="phone" id="phone" tabindex="50" 
         <?php 
         if ($errors || $missing) { 
          echo 'value="' . htmlentities($phone, ENT_COMPAT, 'utf-8') . '"'; 
         } 
         ?>  
         > 
        </p> 
        <p>       
         <label for="contactYou">Contact you by...</label> 
         <?php if ($missing && in_array('contactYou', $missing)) { ?> 
         <div class="warningDivLeft"> 
         <span class="warning">Please select one</span> 
         </div> 
         <?php } ?> 
         <select name="contactYou" size="1" id="contactYou" tabindex="60"> 
          <option value="" selected="selected">- select</option> 
          <option value="email" <?php echo ($contactYou == 'email') ? ' selected="selected"' : ''; ?>>Email</option> 
          <option value="phone" <?php echo ($contactYou == 'phone') ? ' selected="selected"' : ''; ?>>Phone</option> 
         </select> 
        </p> 
       </div> 
       <div id="formRight"> 
        <p> 
         <label for="interest">I am interested in...</label> 
         <?php if ($missing && in_array('interest', $missing)) { ?> 
         <div class="warningDiv"> 
         <span class="warning">Please select one</span> 
         </div> 
         <?php } ?> 
         <select name="interest" size="1" id="interest" tabindex="80"> 
          <option value="" selected="selected">- select</option> 
          <option value="new" <?php echo ($interest == 'new') ? ' selected="selected"' : ''; ?>>Creating a new website</option> 
          <option value="current" <?php echo ($interest == 'current') ? ' selected="selected"' : ''; ?>>Redesigning a current website</option> 
          <option value="responsive" <?php echo ($interest == 'responsive') ? ' selected="selected"' : ''; ?>>Reponsive web design</option> 
          <option value="wordpress" <?php echo ($interest == 'wordpress') ? ' selected="selected"' : ''; ?>>A WordPress website</option> 
          <option value="general" <?php echo ($interest == 'general') ? ' selected="selected"' : ''; ?>>General enquiry</option> 
         </select> 
        </p> 
        <p> 
         <label for="budget">My budget is...</label> 
         <?php if ($missing && in_array('budget', $missing)) { ?> 
         <div class="warningDiv"> 
         <span class="warning">Please select one</span> 
         </div> 
         <?php } ?> 
         <select name="budget" size="1" id="budget" tabindex="90"> 
          <option value="" selected="selected">- select</option> 
          <option value="100" <?php echo ($budget == '100') ? ' selected="selected"' : ''; ?>>€100 - €500</option> 
          <option value="500" <?php echo ($budget == '500') ? ' selected="selected"' : ''; ?>>€500 - €1,000</option> 
          <option value="1000" <?php echo ($budget == '1000') ? ' selected="selected"' : ''; ?>>€1,000 - €2,000</option> 
          <option value="2000" <?php echo ($budget == '2000') ? ' selected="selected"' : ''; ?>>€2,000 - €5,000</option> 
          <option value="5000" <?php echo ($budget == '5000') ? ' selected="selected"' : ''; ?>>€5,000 - €10,000</option> 
          <option value="10000" <?php echo ($budget == '10000') ? ' selected="selected"' : ''; ?>>€10,000+</option> 
         </select> 
        </p> 
        <p> 
         <label for="comments">How can I help you?</label> 
         <?php if ($missing && in_array('comments', $missing)) { ?> 
         <div class="warningDiv"> 
         <span class="warning">Please leave a comment</span> 
         </div> 
         <?php } ?> 
         <textarea name="comments" id="comments" cols="45" rows="5" tabindex="100"><?php 
         if ($errors || $missing) { 
          echo htmlentities($comments, ENT_COMPAT, 'utf-8'); 
         } 
         ?></textarea> 
        </p> 
       </div> 
       <div id="formSubmit"> 
        <ul> 
         <li> 
          <input type="submit" name="submit" id="submit" value="Send Message" tabindex="70"> 
         </li> 
        </ul> 
       </div> 
        </form> 

PHP

<?php 
$errors = array(); 
$missing = array(); 
if (isset($_POST['submit'])) { 
    $to = '[email protected]'; 
    $subject = 'Web Design'; 
    $expected = array('name', 'company', 'email', 'phone', 'contactYou', 'interest', 'budget', 'comments'); 
    $required = array('name', 'email', 'contactYou', 'interest', 'budget', 'comments'); 
    $headers = "From: [email protected]\r\n"; 
    $headers .= "Content-type: text/plain; charset=utf-8"; 
    $authenticate = '[email protected]'; 
    require './_includes/mail_process.php'; 
    if ($mailSent) { 
     header('Location: ' . $_SERVER['REQUEST_URI'] . '?success=1#c'); 
     exit; 
    } 
} 
?> 


<?php if (($_POST && $suspect) || ($_POST && isset($errors['mailfail']))) { ?> 
     <div class="globalWarning"> 
     <p class="warning">Sorry your mail could not be sent</p> 
     </div> 
<?php } elseif ($errors || $missing) { ?> 
     <div class="globalWarning"> 
     <p class="warning">Please fix the item(s) indicated</p> 
     </div> 
<?php } elseif (isset($_GET['success'])) { ?> 
     <div class="globalAlert"> 
     <p class="warning">Thank you! Your message has been sent!</p> 
     </div> 
<?php } ?> 

<?php 
$suspect = false; 
$pattern = '/Content-Type:|Bcc:|Cc:/i'; 

function isSuspect($val, $pattern, &$suspect) { 
    if (is_array($val)) { 
     foreach ($val as $item) { 
      isSuspect($item, $pattern, $suspect); 
     } 
    } else { 
     if (preg_match($pattern, $val)) { 
      $suspect = true;  
     } 
    } 
} 

isSuspect($_POST, $pattern, $suspect); 

if (!$suspect) { 
    foreach ($_POST as $key => $value) { 
     $temp = is_array($value) ? $value : trim($value); 
     if (empty($temp) && in_array($key, $required)) { 
      $missing[] = $key; 
      $$key = ''; 
     } elseif(in_array($key, $expected)) { 
      $$key = $temp; 
     } 
    } 
} 

if (!$suspect && !empty($email)) { 
    $validemail = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL); 
    if ($validemail) { 
     $headers .= "\r\nReply-to: $validemail"; 
    } else { 
     $errors['email'] = true; 
    } 
} 

if (!$suspect && !$missing && !$errors) { 
    $message = ''; 
    foreach ($expected as $item) { 
     if (isset($$item) && !empty($$item)) { 
      $val = $$item; 
     } else { 
      $val = 'Not selected'; 
     } 
     if (is_array($val)) { 
      $val = implode(', ', $val); 
     } 
     $item = str_replace(array('_', '-'), ' ', $item); 
     $message .= ucfirst($item) . ": $val\r\n\r\n"; 
    } 
    $message = wordwrap($message, 70); 

    $mailSent = mail($to, $subject, $message, $headers, $authenticate); 
    if (!$mailSent) { 
     $errors['mailfail'] = true; 
    } 
} 

回答

1

這聽起來像你需要的是一些JavaScript做滾動。我不知道你是否使用jQuery,但它使這很容易。這Stack Overflow link描述了多種方法給它,如:

$.fn.scrollView = function() { 
    return this.each(function() { 
     $('html, body').animate({ 
      scrollTop: $(this).offset().top 
     }, 1000); 
    }); 
} 

,並調用它像:

$('#your-form').scrollView(); 

除此之外,你可能要考慮看​​,如果你可以做任何輸入驗證提交表單之前。看看HTML5的input patternsrequired fields

0

我會把你的錯誤的PHP中的一些JavaScript這樣,當加載因此它運行的JavaScript滾動到正確的位置根據形式ID

DEMO http://jsfiddle.net/RbxVJ/2/

$(function() { 
    $(document).scrollTop($("#header").offset().top); 
}); 

其實我會使用純JavaScript,因爲你可能此時沒有調用過jQuery庫

EDITED

你的PHP

DEMO http://jsfiddle.net/RbxVJ/430/

<?php 
    if(!isset($_GET['success'])) 
    { 
     ?> 
     <script> 
     window.location.hash = '#your-form-ID'; 
     </script> 
     <?php 
    } 
    ?> 
+0

我把它'//確定您已經叫jQuery庫之前沒有在上面的純JavaScript的解決方案需要此point'? – Onimusha

+0

@Onimusha正確,我現在刪除了評論,謝謝。 – Vector

0

如果你要使用JavaScript來解決這個問題,你不妨用它來驗證您的形式,而/除了PHP。它更快(避免使用PHP進行必要的刷新),除了成功提交外,您絕不應該以任何方式重新加載表單。

編寫一個函數,當按下提交按鈕時執行它收集必需的字段,檢查它們是否有適當的值,如果有任何缺失,停止表單提交,然後改變提示文本旁邊的HTML文件規則「就像它一樣。

0
 $(document).ready(function(){ 
     $('#contactBox a[href="' + window.location.hash + '"]').click(); 
    }); 
0

我有類似的問題。當提交當前頁面上的表單時,我會重新加載當前頁面。我的表單位於頁面的底部,但當頁面重新加載時,頁面會再次滾動到頂部。重新加載當前頁面已完成;

<form action=""> 

因此,如果我的表單在我的index.html頁面上,這是相同的;

<form action="index.html"> 

爲了解決滾動問題,我給了表單元素一個id,然後在'action'屬性值中引用了這個id,像這樣;

<form id="contact-form" action="#contact-form"> 

再次,如果表單在我的index.html頁面上,這是相同的;

<form id="contact-form" action="index.html#contact-form"> 

如果你不知道,當你追加一個元素ID的網頁URL的末尾,它會加載該網頁,並直接滾動到該元素。

如果有使用這種方法的任何疑慮,請插入內容。