所以我正在尝试为我的web dev class'最终项目进行有效登录。每当我尝试使用正确的凭据登录时,我总是被重定向到“something.php”页面,并且我从未收到任何输入到代码中的输出。当我输入有效的登录凭据时甚至会发生这种情况。我知道这不是一种安全的登录方式,但这仅用于最终的项目目的。我已将所有文件附在下面(减去图片),即使你可能不需要全部文件。
something.php文件(用于登录的php)
<?php
$action = empty($_POST['action']) ? false : $_POST['action'];
/*var radio = document.getElementById('radio');*/
switch ($action) {
case 'login':
$username = empty($_POST['username']) ? '' : $_POST['username'];
$password = empty($_POST['password']) ? '' : $_POST['password'];
if ($username=='test' && $password=='pass') {
setcookie('userid', $username);
$response = 'Login: Sucess';
}
else {
$response = 'Login: Fail';
}
print $response;
break;
case 'get':
$userid = empty($_COOKIE['userid']) ? '' : $_COOKIE['userid'];
if ($userid=='test') {
$response = 'Todays special are organic Brazilian strawberries $1.75 a pound';
}
if ($username!='test' || $password!='pass'){
header('Location: XXX/something.php');
echo "username and password combo are not valid";
//radio.value = "logged out";
}
print $response;
break;
case 'logout':
setcookie('userid', '', 1);
print 'Logged out';
break;
}
?>
login.html页面
<div class="group">
<div id="radio">
<form id="radio" action="">
<input type="radio" name="select" value="login"> Logged In<br>
<input type="radio" name="select" value="logout" checked> Logged Out<br>
</form>
</div>
<form id="content2" class="itemBlock">
<p> Don't have an account? Sign up here!</p>
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname"><br>
E-mail: <br>
<input type="text" name="email"><br>
Password: <br>
<input type="text" name="password"><br>
<br>
<input type="button" value="Register" onclick="addToCartFxn()">
</form>
<div id="center">
<form id="content" class="itemBlock" action="something.php" method="post">
<br> <br>
E-mail: <br>
<input type="text" name="email"><br>
Password: <br>
<input type="password" name="password"><br>
<input type="submit" class="get" value="Login" id="login">
<input type="submit" value="Logout" id="logout">
</form>
</div>
</div>
final.php页面
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Tabs - Content via Ajax</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<!--<link rel="stylesheet" href="/resources/demos/style.css">-->
<!--<script src="jquery-1.10.2.min.js"></script>-->
<script>
$(function() {
$( "#tabs" ).tabs({
beforeLoad: function( event, ui ) {
ui.jqXHR.fail(function() {
ui.panel.html(
"Couldn't load this tab. We'll try to fix this as soon as possible. ");
});
}
});
});
$(function(){
$('#login').click(function(){
$.post('something.php',
{
action: 'login',
username: $('#username').val(),
password: $('#password').val()
},
function(data){
$('#center').html(data);
});
});
$('#logout').click(function(){
$.post('something.php',
{
action: 'logout'
},
function(data){
$('#center').html(data);
});
});
$('.get').click(function(){
$.post('something.php',
{
action: 'get'
},
function(data){
$('#center').html(data);
});
});
});
function addToCartFxn() {
alert("This feature is coming soon!");
}
</script>
<style>
#floatright {
float:right;
}
#header{
background-image: url("tree rocks header.jpg");
width: 100%;
height: 200px;
padding-top: 1px;
text-align: center;
background-size: cover;
background-position-y: 3255px;
background-position-x: -2112px;
}
#headertext {
z-index: 100;
color: white;
font-size: 72px;
font-family: exo, arial, serif;
}
@font-face {
/* Declare the name of the font (we make this value up) */
font-family: exo;
/* And where it's located */
src: url("Exo-Medium.otf");
}
.addtocart{
background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 7px 12px;
border-radius: 7px;
text-align: center;
text-decoration: none;
font-size: 5px;
margin-bottom: 3px;
}
#radio {
float: right;
}
#content {
font-family: exo, arial, serif;
text-align: center;
border-radius: 25px;
background-color:forestgreen;
align-content: center;
}
#content2 {
font-family: exo, arial, serif;
float:left;
}
#logout{
margin: 5px;
}
#login{
margin: 5px;
}
.itemBlock{
display:inline-block;
margin: 10px;
border: 2px black;
border-style: solid;
text-align: left;
padding-left: 5px;
padding-right: 5px;
}
@font-face {
/* Declare the name of the font (we make this value up) */
font-family: exo;
/* And where it's located */
src: url("Exo-Medium.otf");
}
#center{
margin-left: 42%;
}
body {
min-height: 100%;
height: auto! important;
}
.group:after {
content: "";
display: table;
clear: both;
}
</style>
</head>
<body>
<div id="header">
<p id="headertext"> Claw's Cache</p>
</div>
<div id="tabs">
<ul>
<li><a href="#tabs-1"> Our Products </a></li>
<li><a href="#tabs-2"> Available Produce </a></li>
<li id="floatright"> <a href="login.html"> Login</a></li><!--tabs-3's content is being put there by ajax-->
</ul>
<div id="tabs-1">
<p>Listed here are all the current vendors that are associated with us products.</p>
<?php
//Use glob function to get the files
//Note that we have used " * " inside this function. If you want to get only JPEG or PNG use
//below line and commnent $images variable currently in use
$images = glob("*.png");
$i=0;
//Display image using foreach loop
foreach($images as $image){
echo '<div class="itemBlock"> <a href="'.$image.'" target="_blank"><img src="'.$image.'" height="250" width="200" /></a> <br>';
echo '<button type="button" class="addtocart" onclick="addToCartFxn()"> add to cart</button> </div>';
}
?>
</div>
<div id="tabs-2">
<p>Our available produce page is being updated currently!
<br> <br> As a placeholder, we have attached a video that many of our customers recommend to every person interested in indoor gardening.</p><br>
<iframe width="560" height="315" src="https://www.youtube.com/embed/RWCIaydwM_w" frameborder="0" allowfullscreen></iframe>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
在第
行$action = empty($_POST['action']) ? false : $_POST['action'];
您阅读了一个名为&#39; action&#39;的已发布表单字段。我无法在您的任何网络表单中找到该字段...也许给您的SUBMIT按钮命名是反向的。 Action是Web表单的属性,而不是字段名称。
答案 1 :(得分:0)
看起来它忽略了你的jquery登录代码,因为当你点击Login
时你的表单正在提交,并且因为你正在检查$_POST['action']
是否为空
$action = empty($_POST['action']) ? false : $_POST['action'];
它失败了,因为你没有任何具有该名称的表单元素。 form
的{{1}}属性仅指定发送数据的页面。
我认为您可以尝试将action
添加到onsubmit="return false"
,以使其仅使用您的form
方法。
$.post
答案 2 :(得分:0)
如果您将开头变量赋值修改为
,会发生什么empty($_POST['action']) ? $action = false : $action = $_POST['action'];
我并不是百分之百确定为什么会发生这种情况,但我从过去的经验中知道,简写IF
语句会产生一些意想不到的结果。
除此之外,您似乎无法在任何地方设置$_POST['action']
字段吗?
如果您指的是在开场<form>
标记中指定的传输方法,那么这是错误的。
如果您的简写IF
语句用于确定如何将数据传递到something.php
文件,那么您可以
empty($_POST) ? (empty($_GET) ? echo "No User Data Passed" : $data = $_GET;) : $data = $_POST;
(我知道它看起来很长,但是可以省略对$ _GET数据的额外检查,它只是作为概念类型检查的证据)。
由此,$data
变量将使用其他页面上的表单中的$ _GET或$ _POST用户定义数据进行设置。
我希望这会有所帮助。