从操作页面返回带有 ID 的文章标签

假设你在登录表单中,并且你输入了错误的信息;在这种情况下,你可能希望返回登录页面。

PHP 有一个内置函数 header(),将页面重定向到特定页面。

但是,如果登录页面位于页面底部或中间某处怎么办。

在这种情况下,我们可以定位标签的 id 并将其放入 URL。我们可以将 Html 文章标签用于登录表单的独立项目部分。

使用 PHP 标头返回带有 ID 的特定文章标签

我们使用两种不同的文章标签,它们具有不同的 id,一个带有内容和登录表单。当我们提交时,我们想回到登录部分。

index.php 页面:

<!DOCTYPE html>
<html>
<head>
<style>
p {
  font-size: 100px;
}
#email {
  margin: auto;
  width: 50%;
  border: 3px solid green;
  padding: 10px;
}
input[type=email], input[type=password] {
  width: 100%;
  padding: 10px 18px;
  box-sizing: border-box;
  margin: 8px 0;
  display: inline-block;
  border: 3px solid #8fbc8f ;
}
button {
  background-color: #8fbc8f;
  color: white;
  margin: 8px 0;
  cursor: pointer;
  padding: 10px 18px;
  border: none;
  width: 100%;
}
</style>
</head>
<body>
<div id="main">
    <article id="paragraph1">
        <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam."</P
    </article>
    <article id="email">
        <form action="action.php" method="post">
	        Email: <input type="email" placeholder="example@example.com" >
			<br>
	        Password: <input type="password" placeholder="********">
            <br>
			<button name="submit" type="submit" value="login" class="login">Login</button>
        </form>
    </article>
	<article id="paragraph2">
        <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam."</P
    </article>
</div>
</body>
</html>

表单操作页面,action.php

<script>
  alert("This is form action page; please enter the information again, click ok to go back to the login form");
</script>
<?php
//header function is used to redirect the page to a perticular link. we can fetch the id of a tag and redirect the page
$url = 'Refresh:0; url=index.php#email';
header($url);
?>

index.php 提交表单后,代码将转到第二页并将其重定向回 index.php 的登录部分。

输出:

从操作页面返回带有 ID 的文章标签

在这种情况下,我们可以在 action.php 页面的标头之前放置任何东西来验证数据库中的电子邮件和密码信息,以便有条件地重定向。

对于动态重定向,我们可以在 index.php 页面上使用 javascript 或 jquery 来获取当前标签的 id 并将其发送到 action.php 以将页面重定向到该标签。