我对ajax的调用无效。当我点击提交按钮时,不会调用ajax。我想要做的是当用户点击登录按钮时,进行的调用是为了检查用户名或密码是否匹配。但是这个电话不会去check_user.php 这是我的代码
<html>
<head>
<script type = "text/javascript" src = "jquery.js"></script>
<script type = "text/javascript" src = "jquery_cookie.js"></script>
<script type="text/javascript" src = "chat.js"></script>
<script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('body').append('<div id = "container" class= "logindiv" align = "center">');
$('#container').append('<form id = "form" method="post">');
$('#form').append('<div id = "logcon" class = "loginnext">');
$('#logcon').append('<input type="text" id = "username" name="username" placeholder = "Username" /><br />');
$('#logcon').append('<input type="text" id = "password" name="password" placeholder = "Password" /><br />');
$('#form').append('<input type="submit" id = "submitme" name="submit" value= "Login" />');
$("#form").submit(function(){
alert("U clicked me");
var username = $("#username").val();
var password = $("#password").val();
if(username == null || username == ""){
alert("username cannot be null");
}
else if(password == null || password == ""){
alert("Password cannot be null");
}
else{
alert("In else");
$.ajax({
url: "check_user.php",
type: "POST",
data:{
'sender': username,
'receiver': password,
},
success: function(response){
if(response == 'Y'){
alert("You have logged in successfully");
}
}
});
}
});
});
</script>
</head>
</html>
这是check_user php
<?php
$username = $password = "";
require_once("dc_chat.php");
if (isset($_REQUEST['sender']) AND isset($_REQUEST['sender'])) {
$username = $_REQUEST['sender'];
$password = $_REQUEST['receiver'];
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$sql = "SELECT * from users WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
$result = $mysqli->query($sql);
if (!$result->num_rows == 1) {
echo "N";
}
else {
echo "Y";
}
}
else{
echo "Hello";
}
?>
我没有在这段代码中出错。有人可以帮忙吗。