php - 页面刷新后移动到网页的一部分

标签 php jquery forms validation anchor

我有给定文章的评论列表,并且评论下方有一个表单,供用户添加自己的评论。

我正在使用 php 进行一些表单验证。

这是过程:

  1. 用户填写(或不填写)表单并点击提交按钮。页面刷新。
  2. PHP 验证用户输入,如果没有错误则提交注释,或者 生成错误列表。
  3. 如果存在错误,则显示错误。

    问题是我希望错误显示在表单之前的注释下方,但是当页面刷新时,显示页面顶部,我需要它直接转到错误和表单(很像页面 anchor )

这可能吗?

单击提交按钮后调用此函数

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" );
}
        }
    }

最佳答案

为表单提供一个可以通过 URL 跳转到的 ID:

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

然后将用户重定向回带有适当哈希标记的同一 URL:

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

关于php - 页面刷新后移动到网页的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7783685/

相关文章:

javascript - 我怎样才能取消确认 sweetalert

javascript - 使用 $(document).ready(function() 访问另一个 js 文件中的函数

javascript - JQuery 选择器查找

php - 使用php从sql获取登录用户信息

php - 将 BBCode [IMG] 转换为 <img>

php - Symfony2 子表单事件传播

jquery - 在文本区域中按 Enter 时提交表单

javascript - jQuery/Javascript - X 秒后运行 AJAX 请求

php - 如何在php中设置访问权限以查看页面

javascript - 如何在 jquery 中将分页链接到页面滚动?