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

发布于 2024-12-10 04:13:42 字数 1480 浏览 1 评论 0原文

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

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

流程如下:

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

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

这可能吗?

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

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

I have a list of comments for a given article and I have a form underneath the comments for a user to add their own comments.

I'm using php to do some form validation.

This is the process:

  1. User fills out form (or not) and hits submit button. page refreshes.
  2. PHP validates user input and either submits comments if no errors or
    generates a list of errors.
  3. If errors exist display the errors.

    The problem is that I want the errors to display underneath the comments before the form which it does but when th epage refreshes, the top of the page is displayed and i need it to go straight to the errors and form (much like a page anchor)

Is this possible?

This is called after the submit button is clicked

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;

and this is near the comments and form where i want to page to go to if errors exist

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

演多会厌 2024-12-17 04:13:42

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

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

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

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

Give the form an ID which can be jumped to via the URL:

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

And then redirect the user back to the same URL with the appropriate hash tag:

header('Location: http://www.example.com#submitComment');
我爱人 2024-12-17 04:13:42

找到您的表单标签,它看起来像这样

<form action='yourpage.php'>

在 URL 后面加上一个哈希标签以及提交后它将转到的锚点 -

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

Find your form tag, it will look something like this

<form action='yourpage.php'>

Put a hash tag after the URL along with the anchor it will go to upon submission-

<form action='yourpage.php#commentsform'>
小草泠泠 2024-12-17 04:13:42

使用页面锚点,您可以通过更改 url 中的哈希值将用户跳转到页面的任何部分。

使表单将用户发送到锚点,如下所示:

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

并在您希望用户结束的位置创建一个锚点:

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

Using page anchors you can jump the user to any part of the page by changing the hash in the url.

Make the form send the user to to anchor like so:

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

And make an anchor where you want your user to end up:

<a name="comments"></a> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文