php - 有没有办法在带有 php 的 HTML 中移动和使用几种模式
问题描述
我正在创建一个登录模式,其中将包括一些用于注册、注销和更新注册的模式。处理所有模态的有效方法是什么?我的 HTML 主页是“index.php”,如下所示。
<?php
/*
* Programming Title Block
*/
…
include('account/registration/login_code.php');
?>
<!DOCTYPE html>
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php include("view/head.php"); ?>
</head>
<body class='homePage_bg'>
<?php include("view/header.php"); ?>
<?php include("view/nav.php"); ?>
<div class="content">
<?php include('account/registration/login.php'); ?>
<article>
…
</article>
</div>
<?php include("view/footer.php"); ?>
</body>
</html>
login.php 文件需要做更多的工作,但目前看起来像这样:
<!-- The Modal -->
<div id='id01' class='modal'>
<span onclick="document.getElementById('id01').style.display='none'"
class="close" title="Close Modal">×</span>
<!-- Modal Content -->
<form class="modal-content animate" method='post' action=''>
<div class='modal-header'>
<h2>Member Login</h2>
</div>
<div class="container">
<label><i class="fas fa-user"></i> Name</label>
<input type='text' class='form-control' name='username' />
<label><i class="fas fa-lock"></i> Password</label>
<input type='password' class='form-control' name='password' />
</div>
<div class='modal-footer'>
<input type='submit' class='btn btn-default' name='login' data-dismiss='modal' value='Login' />
<input type='button' class='btn btn-primary' name='cancel' value='Cancel'
onclick="document.getElementById('id01').style.display='none'" />
</div>
</form>
<script>
// Get the modal
var modal = document.getElementById('id01');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target === modal) {
modal.style.display = "none";
}
};
</script>
</div>
“index_code.php”文件如下:
<?php
if(!isset($_SESSION)){
session_start();
}
/*
* Programming Title Block
*/
require_once('model/name_db.php');
$dbna = new dbName();
$_SESSION['$Registration_ID'] = null;
unset($_SESSION['$Registration_ID']);
if(filter_input(INPUT_POST, 'login')) {
// username and password sent from form
$userName = "'" . filter_input(INPUT_POST, 'username') . "'";
$password = "'" . filter_input(INPUT_POST, 'password') . "'";
try {
list($name, $message1) = $dbna->get_name_by_userName_and_password($userName, $password);
$message .= $message1;
if(!$name) {
$message .= "There was no matching User-Name and Password.";
}
else {
$_SESSION["RegName_ID"] = $_SESSION["Name_ID"] = $nameID = $name['name_id'];
$_SESSION["Security_ID"] = $name['security_id'];
$_SESSION["fullName"] = $full_Name = $name['namefull'];
}
} catch(PDOException $e){
$message .= $sql . "<br />" . $e->getMessage().'<br />';
}
header('Location: index.php');
}
?>
以前我有一个与主页 index.php 处于同一级别的 Login.php 文件。在该文件中,我将这些 php 包含在编码的 php 部分和 HTML 部分中。
<?php
…
include 'account/registration/LoginIndex_code.php';
?>
<!DOCTYPE html>
…
<?php include 'account/registration/LoginIndex.php'; ?>
…
The 'account/registration/LoginIndex.php' was as follows:
<?php //
/*
* Programming Title Block
*/
if(filter_input(INPUT_GET, 'action')) {
$action = filter_input(INPUT_GET, 'action');
}
switch ($action) {
case 'login':
include('login.php');
break;
case 'regName':
include('regName.php');
break;
case 'regAddress':
include('regAddress.php');
break;
…
前面代码的问题是我使用 URL 操作变量来更新登录模式的状态。因此,模态不是作为模态,而是作为 HTML 网页。谢谢你看这个。
解决方案
我修改了 nav.php 文件,以便能够登录和用于注销和更新注册的下拉菜单。
...
<?php if(!$full_Name){ ?>
<a class="btn rightBtn" id='login'
onclick="document.getElementById('id01').style.display='block'" >Login</a>
<?php } else { ?>
<div class="dropdown">
<button class="dropbtn">Logout
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a class="btn rightBtn" id='logout'
onclick="document.getElementById('id99').style.display='block'" >Logout</a>
<a class="btn" href="Login.php?action=updateName">Update Name</a>
</div>
</div>
<?php } ?>
...
登录模式现在包括注册选项
...
<div class='modal-footer'>
<input type='submit' class='btn btn-default' name='login' data-dismiss='modal' value='Login' />
<input type='button' class='btn' name='cancel' value='Cancel'
onclick="document.getElementById('id01').style.display='none'" />
<input type='button' class='btn' name='signup' value='Sign-Up'
onclick="location.href='Login.php?action=regName'" />
</div>
...
总之,网站的每个页面现在都包含用于登录和注销的模式代码,通过 onclick 控件将其状态切换为显示或不显示。注册和更新注册由 switch 语句索引,相关代码在根目录下的 Login.php 文件中调用。switch 语句读取 URL 的操作。
推荐阅读
- javascript - React Native webview的onLoad函数什么时候执行?
- mockito - 你如何模拟 JobBuilderFactory?
- angularjs - 基于 HTTP 请求防止 AngularJS 中未经授权的 URL 更改
- c++ - SFINAE 评估中的窄化警告
- javascript - Momentjs 获取日期并设置格式
- r - 如何在 r 中由 ggplotly(来自 ggplot)生成的交互式绘图的工具提示中添加更多信息
- c++ - 在 Mac Mojave 中编译 C++ 时 C 编译器标识未知
- javascript - Javascript - Put array items, including their duplicates, into a new array
- mysql - 使用动态 SQL 存储过程,创建大量结果选项卡
- jsoup - Jsoup 用自闭文本区域弄乱 HTML