我试图向我的数据库发送一个AJAX请求并以JSON格式获取信息,然后我想用那些json信息替换我的div。但我的div不会改变,我不知道为什么..
这是我的代码,非常感谢您的帮助:
的index.php
<form class="login-form" method="post" action="">
<input type="text" name="lusername" placeholder="username"/>
<input type="password" name="lpassword" placeholder="password"/>
<input type="image" name="login" src="src/login.png" onClick="getLoginInfo()">
<p class="message">Not registered? <a href="#" onClick="return false" onmousedown="changeContent('registerClick');"><span id="tab">Create an account</a></span></p>
<div id="test">change me pls</div>
</form>
的login.php
<?php
require_once("connection.php");
if(isset($_POST['login_x']) or isset($_POST['login_y'])) {
$lusername = $_POST['lusername'];
$lpassword = $_POST['lpassword'];
$select = mysqli_query($connection, "SELECT Username, Password FROM Account WHERE Username='".$lusername."' AND Password='".$lpassword."'");
if(mysqli_num_rows($select) == 0) {
echo '<script>cantFindAccount()</script>';
} else {
$array = mysqli_fetch_row($select);
echo json_encode($array);
}
}
?>
getLoginInfo.js
function getLoginInfo() {
$.ajax({
url: 'http://localhost/login.php',
data: "",
dataType: 'json',
success: function(data) {
var user = data[0];
var pass = data[1];
$('#test').html("<b>id: </b>"+user+"<b> name: </b>"+pass);
}
});
return false;
}
答案 0 :(得分:0)
试试这个,您必须将一些数据发布到您的脚本中,以便它可以处理。此外,您必须使用method: 'POST'
,因为您使用了$_POST[]
$.ajax({
url: 'http://localhost/login.php',
data: $(this).serialize(),
method: 'POST',
dataType: 'json',
success: function(data) {
var user = data[0];
var pass = data[1];
$('#test').html("<b>id: </b>"+user+"<b> name: </b>"+pass);
}
});