I am working on a project where i am stuck at an awkward problem. There are two sets of input fields, one for teacher's sign-in and the other for the students. The values in the input fields are passed to a script in the same file and then using ajax i am sending the data to a php file on the localhost. The issue is that there is no problem when i am running it on the browser but after building an apk using phonegap the teacher's sign-in is having problem.
<!-- 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 Functions Used :
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 FILES:
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");
?>
?>
Now, what the above code is supposed to do is, if the username and password is found in the database then it should echo 1 else echo 0 and depending on that the person will be redirected, for instance here if the login is successful then the user will be directed to www.google.com else it will show an error. This is working fine on the browser but when i build the app, while doing teacher's sign-in :
alert(requestTeacher.responseText)
This is showing 0 by default and then its being redirected accordingly. The app is not even allowing me to enter the values while it is working properly on the browser. And this issue is only for the Teacher's section and not for the Student's. Note: Both the sections are on the same page.
I hope you have understood the problem and i request you all to please suggest a possible solution.