我正在开展一个项目,我遇到了一个棘手的问题。 有两组输入字段,一组用于教师登录,另一组用于学生。输入字段中的值传递给同一文件中的脚本,然后使用ajax我将数据发送到localhost上的php文件。问题是当我在浏览器上运行它时没有问题,但在使用phonegap构建apk后,教师的登录有问题。
<!-- Student Login HTML CODE -->
<ul data-role="listview" data-style="inset">
<li>
<label for="username">
Username:</label>
<input type="text" id="loginStudentUsername" /></li>
<li>
<label for="password">
Password:</label>
<input type="password" id="loginStudentPassword" /></li>
</ul>
<a onclick="student_verification('http://192.168.237.1/project/index/Student_Verification.php','harshit')" id="modalview-reg-button" type="button" data-role="button">Login</a>
<!-- Teacher Login HTML -->
<ul data-role="listview" data-style="inset">
<li>
<label for="username">
Username:</label>
<input type="text" id="loginTeacherUsername" /></li>
<li>
<label for="password">
Password:</label>
<input type="password" id="loginTeacherPassword" /></li>
</ul>
<a onclick="teacher_verification('http://192.168.237.1/project/index/Teacher_Verification.php','harshit')" id="modalview-reg-button" type="button" data-role="button">Login</a>
</div>
使用的JavaScript函数:
function student_verification(php_file, tagID) {
var request = get_XmlHttp();
var the_data = 'Name='+document.getElementById('loginStudentUsername').value+'&Password='+document.getElementById('loginStudentPassword').value;
request.open("POST", php_file, true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send(the_data);
request.onreadystatechange = function() {
if (request.readyState == 4) {
if(request.responseText==0)
{
$("#modalview-login4").kendoMobileModalView("close");
openError();
}
else{
window.location.href = "https://www.google.com";
}
}
}
}
function teacher_verification(php_file, tagID) {
var requestTeacher = get_XmlHttp();
var the_data='Name='+document.getElementById('loginTeacherUsername').value+'&Password='+document.getElementById('loginTeacherPassword').value;
requestTeacher.open("POST", php_file, true);
requestTeacher.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
requestTeacher.send(the_data);
requestTeacher.onreadystatechange = function() {
if (requestTeacher.readyState == 4) {
alert(requestTeacher.responseText);// here is the issue in android app which is described below..
if(requestTeacher.responseText==0)
{
$("#modalview-login3").kendoMobileModalView("close");
openError();
}
else{
window.location.href = "https://www.google.com";
}
}
}
}
PHP文件:
Teacher_Verification.php -
<?php
header('Access-Control-Allow-Origin: *');
session_start();
$name = $_POST['Name']; // get data
$pass = $_POST['Password'];
$con=mysqli_connect("192.168.237.1","root","","sams");
if(mysqli_connect_errno()){
//echo mysqli_connect_error();
}
$val = mysqli_query($con,"SELECT * FROM teacher WHERE Name='$name' AND Password='$pass'");
if (mysqli_num_rows($val)==1) {
echo("1");
}
else
{
echo("0");
}
Student_Verification.php:
<?php
header('Access-Control-Allow-Origin: *');
session_start();
$name = $_POST['Name']; // get data
$pass = $_POST['Password'];
$con=mysqli_connect("192.168.237.1","root","","sams");
if(mysqli_connect_errno()){
//echo mysqli_connect_error();
}
$val = mysqli_query($con,"SELECT * FROM student WHERE Name='$name' AND Password='$pass'");
if (mysqli_num_rows($val)=='1') {
echo("1");
}
else
echo("0");
?>
?>
现在,上面的代码应该做的是,如果在数据库中找到用户名和密码,那么它应该回显1其他echo 0并且取决于该人将被重定向,例如,如果登录是成功后,用户将被定向到www.google.com,否则将显示错误。 这在浏览器上工作正常但是当我构建应用程序时,在做老师的登录时:
alert(requestTeacher.responseText)
默认情况下显示0,然后相应地重定向。该应用程序甚至不允许我在浏览器上正常工作时输入值。 这个问题仅适用于教师部分,而不适用于学生部分。 注意:这两个部分都在同一页面上。
我希望您已经理解了这个问题,我请求大家提出一个可能的解决方案。