gpt4 book ai didi

javascript - AJAX 不从 php 返回结果

转载 作者:行者123 更新时间:2023-11-30 12:40:46 26 4
gpt4 key购买 nike

我正在尝试从在线示例中学习,用于使用 php 和 jquery 的登录表单,我使用的是完全相同的示例,但由于某种原因,AJAX 没有返回任何内容,而是将我重定向到另一个 php。这是我一直在尝试的问题的链接。

http://rentaid.info/Bootstraptest/testlogin.html

它应该得到结果并将其显示回同一页面,但它正在将我重定向到另一个包含结果的空白 php。感谢您的宝贵时间,我提供了我拥有的所有代码,我希望这个问题不会太愚蠢。

HTML代码:

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form id= "loginform" class="form-horizontal" action='http://rentaid.info/Bootstraptest/agentlogin.php' method='POST'>
<p id="result"></p>
<!-- Sign In Form -->
<input required="" id="userid" name="username" type="text" class="form-control" placeholder="Registered Email" class="input-medium" required="">
<input required="" id="passwordinput" name="password" class="form-control" type="password" placeholder="Password" class="input-medium">
<!-- Button -->
<button id="signinbutton" name="signin" class="btn btn-success" style="width:100px;">Sign In</button>
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javasript" src="http://rentaid.info/Bootstraptest/test.js"></script>
</body>
</html>

Javascript

$("button#signinbutton").click(function() {
if ($("#username").val() == "" || $("#password").val() == "") {
$("p#result).html("Please enter both userna");
} else {
$.post($("#loginform").attr("action"), $("#loginform:input").serializeArray(), function(data) {
$("p#result").html(data);
});
$("#loginform").submit(function() {
return false;
});
}
});

php

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
ob_start();
session_start();
include 'connect.php';
//get form data
$username = addslashes(strip_tags($_POST['username']));
$password = addslashes(strip_tags($_POST['password']));
$password1 = mysqli_real_escape_string($con, $password);
$username = mysqli_real_escape_string($con, $username);
if (!$username || !$password) {
$no = "Please enter name and password";
echo ($no);
} else {
//log in
$login = mysqli_query($con, "SELECT * FROM Agent WHERE username='$username'")or die(mysqli_error());
if (mysqli_num_rows($login) == 0)
echo "No such user";
else {
while ($login_row = mysqli_fetch_assoc($login)) {

//get database password
$password_db = $login_row['password'];

//encrypt form password
$password1 = md5($password1);

//check password
if ($password1 != $password_db)
echo "Incorrect Password";
else {
//assign session
$_SESSION['username'] = $username;
$_SESSION['password'] = $password1;
header("Location: http://rentaid.info/Bootstraptest/aboutus.html");
}
}
}
}
?>

编辑

$("button#signinbutton").click(function(){

if($("#username").val() ==""||$("#password").val()=="")
$("p#result).html("Please enter both userna");
else


$.post ($("#loginform").attr("action"),
$("#loginform:input").serializeArray(),
function(data) {
$("p#result).html(data); });




});
$("#loginform").submit(function(){
return false;
});

最佳答案

首先,删除:-

header("Location: http://rentaid.info/Bootstraptest/aboutus.html");

如果你想显示数据,回显用户名和密码。

$_SESSION['username'] = $username;
$_SESSION['password'] = $password1;
echo $username."<br>".;
echo $password1;

关于javascript - AJAX 不从 php 返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24526957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com