2011-10-16 55 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>