2011-10-16 54 views
1

我有一個給定文章的評論列表,我有一個表單下的評論爲用戶添加自己的評論。移動到頁面刷新後的網頁的一部分

我使用php來做一些表單驗證。

這是該過程:

  1. 用戶填寫表格(或不)和點擊提交按鈕。頁面刷新。
  2. PHP驗證用戶輸入,並在無錯誤時提交註釋,或者 生成錯誤列表。
  3. 如果存在錯誤,則顯示錯誤。

    問題是,我想要在表單之前顯示錯誤,但是當epage刷新時,頁面的頂部會顯示出來,我需要它直接顯示錯誤和表單(很多像一個頁面錨點)

這可能嗎?

這在點擊提交按鈕

if(empty($errors)){ 
     $result = post_comment('event',$event_id, $sendername, $senderemail, $userurl, $comment); 
     if ($result == 'Correct') { 
      //header('Location: /'.$_SERVER['REQUEST_URI']); 
      header('Location: '.$_SERVER['REQUEST_URI']); 
     } 
     else { 
      $send_error = $result; 

之後被調用,這是評論附近,形成我想要的頁面去,如果存在

// If there was an error sending the email, display the error message 
if (isset($send_error)) { 
echo "<a name=\"commentsform\"></a>"; 
echo "There was an error: ".$send_error; 
} 
/** 
* If there are errors and the number of errors is greater than zero, 
* display a warning message to the user with a list of errors 
*/ 
if (isset($errors) && count($errors) > 0) { 
    echo ("<h2 class='errorhead'>There has been an error:</h2><p><span class='bold'>You forgot to enter the following field(s)</span></p>"); 
    echo ("<ul id='validation'>\n"); 
    foreach ($errors as $error) { 
     echo ("<li>".$error."</li>\n"); 
    } 
echo ("</ul>\n"); 
} 
     } 
    } 
+2

使用跳轉到本地瀏覽器的能力如果url的哈希值匹配,則爲id:'example.com#id'將轉到http://example.com並跳轉到id爲id的元素 –

回答

1

錯誤給形式的ID可以通過URL跳轉到:

<div id="submitComment"> 
    <!-- Comment form here --> 
</div> 

然後重定向用戶回到相同的URL與適當的散列標籤:

header('Location: http://www.example.com#submitComment'); 
+0

對HTML4瀏覽器使用id工作嗎? – 472084

+0

它應該:http://www.w3.org/TR/html401/struct/links.html#h-12.2.3。它是否實際上是另一個問題。 – smack0007

1

找到你的表單標籤,它會是這個樣子

<form action='yourpage.php'> 

將哈希代碼的網址後與錨沿着它會去後送呈

<form action='yourpage.php#commentsform'> 
1

使用頁面定位符,您可以通過更改url中的散列值將用戶跳轉到頁面的任何部分。

製作形式向用戶發送到錨定像這樣:

<form action='yourpage.php#comments'> 

而且使您希望您的用戶結束了一個錨:

<a name="comments"></a>