Php 登录表单验证与jQuery问题
我试图用jQuery和php验证表单,我已经学习了一些教程,但没有一个对我有用。我真的不知道发生了什么,我尝试过几种不同的方法,所以我希望有人知道问题是什么,为什么会发生,以及如何解决 所以。。。这是我的login.phpPhp 登录表单验证与jQuery问题,php,jquery,html,Php,Jquery,Html,我试图用jQuery和php验证表单,我已经学习了一些教程,但没有一个对我有用。我真的不知道发生了什么,我尝试过几种不同的方法,所以我希望有人知道问题是什么,为什么会发生,以及如何解决 所以。。。这是我的login.php <?php $meta_description = "Ingrese a su cuenta para poder realizar diferentes operaciones"; $page_title = "Login - Estudio Beni
<?php
$meta_description = "Ingrese a su cuenta para poder realizar diferentes operaciones";
$page_title = "Login - Estudio Benintendi";
include("includes/header.php")
?>
<form method="post" action="">
<fieldset>
<legend>Ingrese los siguientes datos para logearse</legend>
<div id="resultado"></div>
<label>Nombre de usuario</label>
<div class="input-control text" data-role="input-control">
<input type="text" id="username" placeholder="Ingrese su nombre de usuario..." required />
<button class="btn-clear" tabindex="-1" type="button"></button>
</div>
<label>Contraseña</label>
<div class="input-control password" data-role="input-control">
<input type="password" id="password" placeholder="Ingrese su contraseña..." autofocus required />
<button class="btn-reveal" tabindex="-1" type="button"></button>
</div>
<div class="input-control checkbox" data-role="input-control">
<label>
<input type="checkbox" checked>
<span class="check"></span>
Recordarme
</label>
</div>
<p><button id="submit">Enviar</button></p>
</fieldset>
</form>
<?php include("includes/footer.php") ?>
下面是我的php脚本,它实际连接到数据库
<?php
require_once("config.php");
session_start();
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE) or die ("No se pudo conectar a la base de datos");
$username = $_POST["username"];
$password = $_POST["password"];
$sql = "SELECT Nombre FROM usuarios WHERE Nombre='$username' AND Password='$password'";
$result = $conn->query($sql);
if($result->num_rows == 1)
{
$_SESSION["username"] = $username;
echo "true";
}
else
{
echo "false";
}
$conn->close();
?>
为您的js尝试以下操作:
$(function(){
$('#submit').click(function(){
queryString = $(this).parents('form:first').serialize(); //serialize all valid form inputs/controls
$.post('/includes/validar_login.php',queryString, function(data){
var resp = JSON.parse(data);
if(resp == "true")
{
window.location = "index.php";
}else{
$("#resultado").html(data);
}
});
});
});
```
在您的validar_login.php中
将echo“true”和echo“false”更改为die(json_encode('true'))和die(json_encode('false'))将缓存设置为false,将数据类型设置为json
$("#submit").on("click", function(){
var obj = {'username':$("#username").val(),'password':$("#password").val()};
Post(obj);
});
function Post(arr) {
console.log(arr);
$.ajax({
type: "POST",
url: "/includes/validar_login.php",
data: arr,
cache: false,
dataType: "json",
success: function (response) {
console.log(response);
}
});
}
对于您的php:
<?php
$username = $_POST["username"];
$password = $_POST["password"];
$response = array('username'=>$username,'password'=>$password);
echo json_encode($response);
Add:我尝试过ajax“成功”,但也没有成功!我在.htaccess中也有一些重写规则,这会导致问题吗?我不知道。但我想不会。@N3HL这个简单的设置应该可以用于测试。我今天刚刚做了这个。我唯一得到的是“未捕获的syntaxerror意外令牌函数”干净的控制台,您可以在
$("#submit").on("click", function(){
var obj = {'username':$("#username").val(),'password':$("#password").val()};
Post(obj);
});
function Post(arr) {
console.log(arr);
$.ajax({
type: "POST",
url: "/includes/validar_login.php",
data: arr,
cache: false,
dataType: "json",
success: function (response) {
console.log(response);
}
});
}
<?php
$username = $_POST["username"];
$password = $_POST["password"];
$response = array('username'=>$username,'password'=>$password);
echo json_encode($response);