页面刷新后移动到网页的一部分
我有给定文章的评论列表,并且评论下方有一个表单,供用户添加自己的评论。
我正在使用 php 进行一些表单验证。
流程如下:
- 用户填写(或不填写)表单并点击提交按钮。页面刷新。
- 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" );
}
}
}
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:
- User fills out form (or not) and hits submit button. page refreshes.
- PHP validates user input and either submits comments if no errors or
generates a list of errors. 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为表单提供一个可以通过 URL 跳转到的 ID:
然后将用户重定向回带有适当哈希标签的相同 URL:
Give the form an ID which can be jumped to via the URL:
And then redirect the user back to the same URL with the appropriate hash tag:
找到您的表单标签,它看起来像这样
在 URL 后面加上一个哈希标签以及提交后它将转到的锚点 -
Find your form tag, it will look something like this
Put a hash tag after the URL along with the anchor it will go to upon submission-
使用页面锚点,您可以通过更改 url 中的哈希值将用户跳转到页面的任何部分。
使表单将用户发送到锚点,如下所示:
并在您希望用户结束的位置创建一个锚点:
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:
And make an anchor where you want your user to end up: