我有PHP AJAX登录脚本。它几乎完成了,它的工作方式是用户输入他们的信息,然后将他们带到登录页面。我的问题是AJAX文件的输入没有发送到PHP文件。
它没有给我任何错误。
这是代码:
HTML:`
<div class="form-group">
<div class="col-sm-5">
<input type="text" class="form-control" name="userName" id="userName" placeholder="Your username">
</div>
</div>
<div class="form-group">
<div class="col-sm-5">
<input type="text" class="form-control" name="passWord" id="passWord" placeholder="Your password">
</div>
</div>
<div class="form-group">
<div class="col-sm-5">
<input type="button" onclick="submitRef()" class="form-control" value="Login">
</div>
</div>
<div id="Divvy"></div>
</form>`
这是AJAX / Jquery
<script type="text/javascript">
function submitRef(){
if(userName == "" ){
alert("You must enter something");
}else{
$.ajax({
type: 'post',
url: 'adminLoginProcess.php',
cache: false,
data: {
userName: $("#userName").val(),
passWord: $("#passWord").val()
},
success: function (response) {
window.location.href="adminLoginProcess.php";
}
});
}
}
</script>
这是PHP
`
<?php
session_start();
include "conn.php";
$userName = $_POST['userName'];
$passWord = md5($_POST['passWord']);
echo $userName;
echo "</br>";
echo "</br>";
echo $passWord;
$query = "SELECT uname, pass FROM Login WHERE uname = '$userName' AND pass = '$passWord'";
$result = mysqli_query($conn, $query) or die (mysqli_error());
$num_rows = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
if($num_rows == 1){
$_SESSION['userName'] = $userName;
echo $userName;
echo "logged in";
}else{
echo "Wrong details";
}
$conn->close();
?>
`
答案 0 :(得分:0)
变量&#39; userName&#39;未在函数范围内定义。它应该是这样的。试试这个,看看它是否有帮助。
if($(&#34;#userName&#34;)。val()==&#34;&#34;){
答案 1 :(得分:0)
更改此代码:
<form id="main_form">
<div class="form-group">
<div class="col-sm-5">
<input type="text" class="form-control" name="userName" id="userName" placeholder="Your username">
</div>
</div>
<div class="form-group">
<div class="col-sm-5">
<input type="text" class="form-control" name="passWord" id="passWord" placeholder="Your password">
</div>
</div>
<div class="form-group">
<div class="col-sm-5">
<input type="button" id="form_submit_btn" class="form-control" value="Login">
</div>
</div>
<div id="Divvy"></div>
</form>
和js代码:
$(document).on("click", "#form_submit_btn", function(e) {
$.ajax({
type: 'post',
url: 'adminLoginProcess.php',
cache: false,
data: $("#main_form").serialize(),
success: function (response) {
window.location.href="adminLoginProcess.php";
}
});
});