我有一个简单的ajax登录页面,如下所示。我试图让它工作4天,但不知道为什么它不起作用。 即使我输入有效的电子邮件ID和名称,它也会显示无效的凭据,我也不知道我做错了什么。
这是我的代码:
的login.php
<?php
session_start();
$mysqli = mysqli_connect("localhost","root","","ajax1");
?>
<!DOCTYPE HTML>
<html>
<head>
<title> login script with ajax</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body style="background-color:#b3ffff">
<div style="padding-left:500px ; padding-top:200px">
Name: <input id="name" type="text" name="name" placeholder="Enter name"><br><br>
E-mail: <input id="email" type="email" name="email" placeholder="Enter E-mail"><br><br>
<input id="submit" name="submit" type="button" value="Log In">
<p style="color:black">Havent Registered? <a href="index.php">Register</a>.</p><br><br>
<div id="display" style="color:red"></div>
<script>
$(document).ready(function(){
$("#submit").click(function(){
var name = $("#name").val();
var email = $("#email").val();
var datastring = 'name=' + name + '&email=' + email;
if(name=='' || email==''){
$("#display").html("Please Enter All The Fields");
}
else{
$.ajax({
type: "POST",
url: "success.php",
data: datastring,
cache: false,
success: function(result){
$("#display").html(result);
window.location = "welcome.php";
}
});
}
return false;
});
});
</script>
</div>
</body>
</html>
success.php
<?php
session_start();
$mysqli = mysqli_connect("localhost","root","","ajax1");
if (isset($_SESSION['id'])){
header('location:welcome.php');
}
if (isset($_POST['submit'])){
// removes backslashes
$name = stripslashes($_REQUEST['name']);
//escapes special characters in a string
$name = mysqli_real_escape_string($mysqli,$name);
// removes backslashes
$email = stripslashes($_REQUEST['email']);
//escapes special characters in a string
$email = mysqli_real_escape_string($mysqli,$email);
//Checking is user existing in the database or not
$query = "SELECT * FROM `users` WHERE name='$name' and email='$email'";
$result = mysqli_query($mysqli,$query);
$row1 = mysqli_fetch_array($result);
$rows = mysqli_num_rows($result);
if($rows==1){
$_SESSION['name'] = $name;
$_SESSION['id']=$row1['userid'];
echo 'Logged in Successfully '
}
else{
echo ' Invalid Name or E-MAIL ';
}
}
?>
的welcome.php
<?php
session_start();
if (!isset($_SESSION['id'])) {
header('location:login.php');
}
?>
<!DOCTYPE html>
<html>
<body>
<?php
include_once 'connect.php';
$query=mysqli_query($mysqli,"select * from `users` where userid='".$_SESSION['id']."'");
$row=mysqli_fetch_array($query);
echo 'Welcome - '.$row['username'];
?>
<!--br>
<a href="logout.php">Logout</a>
<br><br-->
</body>
</html>
答案 0 :(得分:0)
您确实需要尽可能简单地进行故障排除。然后当你开始工作时,慢慢地向它添加更多代码,直到它完成所有工作。一步一步。
将success.php页面更改为:
<?php
$myPostArray = array(
'name' => $_POST['name'],
'email' => $_POST['email']
);
echo json_encode($myPostArray);
?>
login.php到此:
<!DOCTYPE HTML>
<html>
<head>
<title> login script with ajax</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body style="background-color:#b3ffff">
<div style="padding-left:500px ; padding-top:200px">
Name: <input id="name" type="text" name="name" placeholder="Enter name" required><br><br>
E-mail: <input id="email" type="email" name="email" placeholder="Enter E-mail" required><br><br>
<input id="submit" name="submit" type="button" value="Log In">
<p style="color:black">Havent Registered? <a href="index.php">Register</a>.</p><br><br>
<div id="display" style="color:red"></div>
</div>
<script>
$(document).ready(function(){
$("#submit").click(function(){
var name = $("#name").val();
var email = $("#email").val();
if(name != '' && email != ''){
var datastring = 'name=' + name + '&email=' + email;
$.ajax({
url: "success.php",
type: "POST",
cache: false,
data: datastring,
dataType: 'json',
success: function(data){
console.log(data); //This will show what you get back from success.php.
if (data) {
//It works, do something.
alert('Here is your response from the php page:' + response);
console.log('Here is your response from the php page:' + 'Name: ' + data['name'] + ' ' + 'Email: ' + data['email']);
} else{
//It does not work, do something.
alert('Your ajax failed to get the info from your php page. Keep troubleshooting');
console.log('Your ajax failed to get the info from your php page. Keep troubleshooting');
}
}
});
}else{
alert('You must enter a valid name and email.');
}
});
});
</script>
</body>
</html>
你真的需要使用你的控制台来帮助你解决这个问题。您可以获得更详细的信息。停止使用alert()进行故障排除。
答案 1 :(得分:0)
您的AJAX和PHP代码都需要一些修复。还尝试在登录时使用密码而不是仅使用电子邮件和名称。尝试修复您的AJAX代码和PHP代码。
在AJAX中使用以下代码。
echo '1'
PHP中的:
而不是$ _REQUEST ['name'],$ _REQUEST ['email']尝试使用$ _POST ['name']和$ _POST ['email']
然后echo '0'
表示成功,或import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Football App', // app name here
home: new TesterClass(), // home page loader here
);
}
}
class TesterClass extends StatefulWidget{
TesterClassState createState() => new TesterClassState();
}
class TesterClassState extends State<TesterClass>{
int index = 0;
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(
appBar: new AppBar(
title: new Text("Tester"),
centerTitle: true,
),
bottomNavigationBar: new BottomNavigationBar(
type: BottomNavigationBarType.shifting,
currentIndex: index,
items: [
index == 0? new NavigationButtonView(Icons.home,
"Home").activeButtonBuilder():new NavigationButtonView(Icons.home,
"Home").inactiveButtonBuilder(),
index == 1? new NavigationButtonView(Icons.search,
"PL").activeButtonBuilder():new NavigationButtonView(Icons.search,
"PL").inactiveButtonBuilder(),
index == 2? new NavigationButtonView(Icons.search,
"Fantasy").activeButtonBuilder():new NavigationButtonView(Icons.search,
"Fantasy").inactiveButtonBuilder(),
],
onTap: (int i){setState(() {
index = i;
});},
),
);
}
}
class NavigationButtonView{
IconData _icon;
String _textLabel;
TextStyle activeTextStyle;
TextStyle inactiveTextStyle;
Color inactiveIconColor;
Color activeIconColor;
Color activeBackgroundColor;
Color inactiveBackgroundColor;
NavigationButtonView(
IconData icon,
String textLabel,
){
_icon = icon;
_textLabel = textLabel;
}
BottomNavigationBarItem activeButtonBuilder(){
return new BottomNavigationBarItem(
icon:new Icon(_icon,
color: activeIconColor,),
title:new Text(
_textLabel,
style: activeTextStyle,
),
backgroundColor: activeBackgroundColor
);
}
BottomNavigationBarItem inactiveButtonBuilder(){
return new BottomNavigationBarItem(
icon: new Icon(
_icon, color: inactiveIconColor,
),
title: new Text(_textLabel,
style: inactiveTextStyle,
),
backgroundColor: inactiveBackgroundColor
);
}
}`
表示失败。
尝试在块bby块中构建它并在开发时检查错误