我有给定文章的评论列表,并且评论下方有一个表单,供用户添加自己的评论。
我正在使用 php 进行一些表单验证。
这是过程:
- 用户填写(或不填写)表单并点击提交按钮。页面刷新。
- PHP 验证用户输入,如果没有错误则提交注释,或者 生成错误列表。
如果存在错误,则显示错误。
问题是我希望错误显示在表单之前的注释下方,但是当页面刷新时,显示页面顶部,我需要它直接转到错误和表单(很像页面 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/