我的 index.php 页面是这样的:
if(isset($_GET['page'])) {
switch ($_GET['page']) {
case 'login':
include('login.php');
break;
case 'log_out':
$_SESSION['loggedin'] = false;
include('loggedout.html');
break;
case 'profile':
include('profile.php');
break;
case 'contact_us':
include('contact.html');
break;
default:
include('home.html');
break;
}
} else {
include('home.html');
}
现在,当按下链接或提交表单时,我可以轻松地调用新页面,即:
<a href="index.php?page=home">Home</a> <a href="index.php?page=contact_us">Contact Us</a>
但是如果我有一个多合一的形式怎么办......它需要能够通过 PHP 代码向我发送请求index.php
以显示登录页面或重新显示login.php
页面,但我似乎找不到任何可以让我在 php 脚本中发送请求,即:
<?php
if(..) {
send request to index.php i.e index.php?page=profile
}
?>
我需要这个的原因是我index.php
用作divs
格式化,因此如果我不通过index.php
它们请求页面,它们将显示在一个新的 html 中(即它没有我的导航栏等)
根据@HydraIO 和其他人的评论,这将如何使用 JQuery 完成?
这是一些代码:
lgoin.php:
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1"
/>
<!--
//thanks http://decoraora.com/fototapete_115x175.php for the image http://decoraora.com/fototapete/images/664.jpg -->
<style type="text/css">
.table {
background: url("bg.jpg") no-repeat;
width: 600px;
height: 250px;
}
</style>
</head>
<body>
<?php
if (isset($_POST['formLogin'])) {
$un = $_POST['username'];
$pwd = $_POST['password'];
if($pwd!=''&&$un!='') {
if (md5($pwd)==='29ef52e7563626a96cea7f4b4085c124') {
$_SESSION['loggedin'] = true;
$_SESSION['login_error'] = 0;
$_SESSION['username'] = $un;
$_SESSION['password'] = $pwd;
//include('profile.php');
//header("Location: index.php/page=profile");
//header("Location: http://localhost/index.php?index.php?page=profile");
} else {
$_SESSION['login_error'] = 1;
echo 'this will dipslay in its own page';
//include('login.php');
}
}else {
$_SESSION['login_error'] = 2;
echo 'display in its own page';
//include('login.php');
?>
<form action="login.php" method="post">
<table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
<tr>
<td colspan="2" align="center">Login for CC Chat</td>
</tr>
<tr>
<td>Username:</td>
<td>Password:</td>
</tr>
<tr>
<td><input type="text" name="username" maxlength="50"/></td>
<td><input type="password" name="password" maxlength="50"/></td>
</tr>
<tr>
<td><a href="index.php?page=contact_us">Forgot your password?</a></td>
<td><input type="submit" name="formLogin" value="Login"/></td>
</tr>
<?php
if(isset($_SESSION['login_error'])) {
switch ($_SESSION['login_error']) {
case 1:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password does not match</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
case 2:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password and/or Username cannot be empty</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
default:
break;
}
}
?>
<tr>
<td colspan="2" align="center">© CopyLeft David Kroukamp 11039662 2013 - 2015</td>
</tr>
</table>
</form>
<?php
}
}else {
?>
<form action="login.php" method="post">
<table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
<tr>
<td colspan="2" align="center">Login for CC Chat</td>
</tr>
<tr>
<td>Username:</td>
<td>Password:</td>
</tr>
<tr>
<td><input type="text" name="username" maxlength="50"/></td>
<td><input type="password" name="password" maxlength="50"/></td>
</tr>
<tr>
<td><a href="index.php?page=contact_us">Forgot your password?</a></td>
<td><input type="submit" name="formLogin" value="Login"/></td>
</tr>
<?php
if(isset($_SESSION['login_error'])) {
switch ($_SESSION['login_error']) {
case 1:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password does not match</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
case 2:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password and/or Username cannot be empty</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
default:
break;
}
}
?>
<tr>
<td colspan="2" align="center">© CopyLeft David Kroukamp 11039662 2013 - 2015</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
索引.php:
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>CC Chat</title>
<meta
http-equiv="content-type" content="text/html; charset=iso-8859-1"
/>
<!--
Thanks too http://stackoverflow.com/a/7851347/1133011 for the help
on layout which acts more like frames but avoids them and using divs. As frames have
known draw backs see here http://stackoverflow.com/questions/4600648/frames-with-php we
should thus rather use include in php
!-->
<style type="text/css" media="screen">
/* Generic pane rules */
body { margin: 0 }
.row, .col { overflow: hidden; position: absolute; }
.row { left: 0; right: 0; }
.col { top: 0; bottom: 0; }
.scroll-x { overflow-x: auto; }
.scroll-y { overflow-y: auto; }
.header.row { height: 75px; top: 0; background-color: #E5E5E5; }
.menu.row { height: 75px; top: 75px; background-color: #EDEDED; }
.body.row { top: 150px; bottom: 50px; background-color: #F5F5F5; }
.footer.row { height: 75px; bottom: 0; background-color: #EBEBEB; }
A:link {text-decoration: none}
A:visited {text-decoration: none}
A:active {text-decoration: none}
A:hover {font-size:24; font-weight:bold; color: red;}
</style>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<div class="header row">
<?php
include("header.html");
?>
</div>
<div class="body row scroll-y">
<?php
if(isset($_GET['page'])) {
switch ($_GET['page']) {
case 'login':
include('login.php');
break;
case 'log_out':
$_SESSION['loggedin'] = false;
include('loggedout.html');
break;
case 'profile':
include('profile.php');
break;
case 'contact_us':
include('contact.html');
break;
default:
include('home.html');
break;
}
} else {
include('home.html');
}
?>
</div>
<div class="menu row">
<?php
include("nav.php");
?>
</div>
<div class="footer row">
<?php
include("footer.php");
?>
</div>
</body>
</html>
在检查密码哈希的 if 语句中使用 JQuery 尝试更新:
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1"
/>
<!--
//thanks http://decoraora.com/fototapete_115x175.php for the image http://decoraora.com/fototapete/images/664.jpg -->
<style type="text/css">
.table {
background: url("bg.jpg") no-repeat;
width: 600px;
height: 250px;
}
</style> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<?php
if (isset($_POST['formLogin'])) {
$un = $_POST['username'];
$pwd = $_POST['password'];
if($pwd!=''&&$un!='') {
if (md5($pwd)==='29ef52e7563626a96cea7f4b4085c124') {
$_SESSION['loggedin'] = true;
$_SESSION['login_error'] = 0;
$_SESSION['username'] = $un;
$_SESSION['password'] = $pwd;
//include('profile.php');
//header("Location: index.php/page=profile");
//header("Location: http://localhost/index.php?index.php?page=profile");
//exit();
echo "<script type='text/javascript'>";
echo "$.get('index.php?page=profile', function(data) {
$('.body.row').html(data);
alert('Load was performed.');
});";
echo "</script>";
} else {
$_SESSION['login_error'] = 1;
echo 'this will dipslay in its own page';
//include('login.php');
}
}else {
$_SESSION['login_error'] = 2;
echo 'display in its own page';
//include('login.php');
?>
<form action="login.php" method="post">
<table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
<tr>
<td colspan="2" align="center">Login for CC Chat</td>
</tr>
<tr>
<td>Username:</td>
<td>Password:</td>
</tr>
<tr>
<td><input type="text" name="username" maxlength="50"/></td>
<td><input type="password" name="password" maxlength="50"/></td>
</tr>
<tr>
<td><a href="index.php?page=contact_us">Forgot your password?</a></td>
<td><input type="submit" name="formLogin" value="Login"/></td>
</tr>
<?php
if(isset($_SESSION['login_error'])) {
switch ($_SESSION['login_error']) {
case 1:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password does not match</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
case 2:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password and/or Username cannot be empty</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
default:
break;
}
}
?>
<tr>
<td colspan="2" align="center">© CopyLeft David Kroukamp 11039662 2013 - 2015</td>
</tr>
</table>
</form>
<?php
}
}else {
?>
<form action="login.php" method="post">
<table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
<tr>
<td colspan="2" align="center">Login for CC Chat</td>
</tr>
<tr>
<td>Username:</td>
<td>Password:</td>
</tr>
<tr>
<td><input type="text" name="username" maxlength="50"/></td>
<td><input type="password" name="password" maxlength="50"/></td>
</tr>
<tr>
<td><a href="index.php?page=contact_us">Forgot your password?</a></td>
<td><input type="submit" name="formLogin" value="Login"/></td>
</tr>
<?php
if(isset($_SESSION['login_error'])) {
switch ($_SESSION['login_error']) {
case 1:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password does not match</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
case 2:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password and/or Username cannot be empty</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
default:
break;
}
}
?>
<tr>
<td colspan="2" align="center">© CopyLeft David Kroukamp 11039662 2013 - 2015</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
我看到了警报,但像以前一样被重定向到空白 html(甚至比以前至少重新加载表单