我无法登录位于PC服务器的数据库。 我使用Cordova / Phonegap。
那是我的代码。
HTML:
<head>
<title>Login Form Using jQuery - Demo Preview</title>
<meta name="robots" content="noindex, nofollow">
<!-- Include CSS File Here -->
<link rel="stylesheet" href="css/index.css"/>
<!-- Include CSS File Here -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="js/login.js"></script>
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
</head>
<body style="background-color: red;">
<div class="container">
<div class="main">
<form class="form" method="post" action="#">
<h2>Create Login Form Using jQuery</h2>
<label>Email :</label>
<input type="text" name="demail" id="email">
<label>Password :</label>
<input type="password" name="password" id="password">
<input type="button" name="login" id="login" value="Login">
</form>
</div>
</div>
</body>
</html>
login.js:
$(document).ready(function () {
$("#login").click(function () {
var email = $("#email").val();
var password = $("#password").val();
// Checking for blank fields.
if (email === '' || password === '') {
$('input[type="text"],input[type="password"]').css("border", "2px solid red");
$('input[type="text"],input[type="password"]').css("box-shadow", "0 0 3px red");
alert("Please fill all fields...!!!!!!");
} else {
$.post("http://192.168.0.110/login.php", {email1: email, password1: password},
function (data) {
if (data === 'Invalid Email.......') {
$('input[type="text"]').css({"border": "2px solid red", "box-shadow": "0 0 3px red"});
$('input[type="password"]').css({"border": "2px solid #00F5FF", "box-shadow": "0 0 5px #00F5FF"});
alert(data);
} else if (data === 'Email or Password is wrong...!!!!') {
$('input[type="text"],input[type="password"]').css({"border": "2px solid red", "box-shadow": "0 0 3px red"});
alert(data);
} else if (data === 'Successfully Logged in...') {
$("form")[0].reset();
$('input[type="text"],input[type="password"]').css({"border": "2px solid #00F5FF", "box-shadow": "0 0 5px #00F5FF"});
alert(data);
} else {
alert(data);
}
});
}
});
});
login.php(在我的localhost上)
<?php
$connection = mysql_connect("localhost", "root", ""); // Establishing connection with server..
$db = mysql_select_db("college", $connection); // Selecting Database.
$email=$_POST['email1']; // Fetching Values from URL.
$password= sha1($_POST['password1']); // Password Encryption, If you like you can also leave sha1.
// check if e-mail address syntax is valid or not
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // sanitizing email(Remove unexpected symbol like <,>,?,#,!, etc.)
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
echo "Invalid Email.......";
}else{
// Matching user input email and password with stored email and password in database.
$result = mysql_query("SELECT * FROM registration WHERE email='$email' AND password='$password'");
$data = mysql_num_rows($result);
if($data==1){
echo "Successfully Logged in...";
}else{
echo "Email or Password is wrong...!!!!";
}
}
mysql_close ($connection); // Connection Closed.
?>
当我点击登录按钮时,没有任何反应。 我尝试添加一个警报,但它在$ .post()中但没有显示。
任何想法的人?
提前致谢。 :d
答案 0 :(得分:0)
将此$.post
替换为此
$.ajax({
type: 'POST',
url: 'http://192.168.0.110/login.php',
data:{email1: email, password1: password},
success: function(data) {
alert("from server"+data);
},
error:function(err){
alert("error"+JSON.stringify(err));
}
});
并跟踪error
函数的输出,这将有助于我们找到错误的错误