我有一个评论列表为给定的文章,我有一个形式下面的评论,供用户添加自己的评论。
我正在使用php进行一些表单验证。
这是一个过程:
问题是,我希望将错误显示在表单之前的注释下面,但当它刷新时,页面的顶部就会显示出来,我需要它直接进入错误和表单(就像页面锚)。
这个是可能的吗?
这是在单击提交按钮后调用的。
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" );
}
}
}
发布于 2011-10-16 10:33:31
给表单一个可以通过URL跳到的ID:
<div id="submitComment">
<!-- Comment form here -->
</div>
然后使用适当的哈希标记将用户重定向回同一个URL:
header('Location: http://www.example.com#submitComment');
发布于 2011-10-16 10:34:31
找到您的表单标签,它将如下所示
<form action='yourpage.php'>
在URL之后放置一个散列标签,以及它在提交时将到达的锚点-
<form action='yourpage.php#commentsform'>
发布于 2011-10-16 10:58:18
使用页面锚点,您可以通过更改url中的散列将用户跳到页面的任何部分。
使表单将用户发送到锚点如下:
<form action='yourpage.php#comments'>
并在您希望用户结束的地方设置一个锚:
<a name="comments"></a>
https://stackoverflow.com/questions/7783685
复制相似问题