Jump to content

php basic ajax example, not returning 0 value from php file


PHP_CHILD

Recommended Posts

i have a very basic ajax script. It works for if login is successful, but the not when the login is not ok.

 

<script type="text/javascript">


       // Wait for the page to load first
       window.onload = function() {


          function createObject() {
 var request_type;
 var browser = navigator.appName;
   if(browser == "Microsoft Internet Explorer"){
   request_type = new ActiveXObject("Microsoft.XMLHTTP");
   }else{
   request_type = new XMLHttpRequest();
   }
 return request_type;
 }


var http = createObject();


         var a = document.getElementById("login");


         //Set code to run when the link is clicked
         // by assigning a function to "onclick"
         a.onclick = function() {





document.getElementById('login_response').innerHTML = "Loading..."


var username = encodeURI(document.getElementById('username').value);
var password = encodeURI(document.getElementById('password').value);


nocache = Math.random();


http.open('get', 'http://appsmarketing.net/hr/login.php?username='+username+'&password='+password+'&nocache = '+nocache);
http.onreadystatechange = loginReply;
http.send(null);
 }
function loginReply() {
if(http.readyState == 4){ 
var response = http.responseText;
if(response == 0){
document.getElementById('login_response').innerHTML = 'Login failed! Verify user and password';
// else if login is ok show a message: "Welcome + the user name".
} else {
document.getElementById('login_response').innerHTML = 'Welcome'+response;

}
  }
           return false;
         }
       }
   </script>
</head>


<body>
  <form method="get">
  <tbody>
  <tr>
                       <td >Username: </td>
                       <td ><input type="text" name="username" value="" id="username" class="txt_bg"></td>
                    </tr>
  <tr>
                       <td>Password: </td>
                       <td ><input type="password" name="password" id="password" value="" class="txt_bg"></td>
                    </tr>
  <tr>
                    <input id="login" type="submit" value="Login" class="login_btn"></a></div>
  </tr>
  </tbody>
  </table>
  </form>

and the php file is

 

 

<?php





if(isset($_GET['username']) && isset($_GET['password'])){


$uname = $_GET['username'];
$psw = $_GET['password'];


$aa="SELECT 'mid' FROM `check` WHERE uname='".$uname."' AND password123='".$psw."'  AND mid='1'";


$query=mysql_query($aa) or die(mysql_error());


$num_results=mysql_num_rows($query) or die(mysql_error());


if(($query)&&($num_results==1))



{echo "ok done";} 
else 
{echo "0";}




}


?>

i want to make response==0, but the control transfers to the else part of javascript function?? how do i return the false value to get login failed message.. Thanks in advances...

When you execute the php directly in your browser instead of via AJAX, what do you get?

sorry for d late reply..i get nothin actually from login.php... no output.. d success case works fine. am havin trouble with not-success part.. i get Welcome ok done for success. and for not-sucess i get just Welcome...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.