Jump to content


Photo

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

php ajax

  • Please log in to reply
4 replies to this topic

#1 PHP_CHILD

PHP_CHILD

    Advanced Member

  • Members
  • PipPipPip
  • 30 posts

Posted 11 February 2013 - 10:33 AM

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...

#2 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,377 posts
  • LocationCheshire, UK

Posted 11 February 2013 - 10:53 AM

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

moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#3 PHP_CHILD

PHP_CHILD

    Advanced Member

  • Members
  • PipPipPip
  • 30 posts

Posted 12 February 2013 - 04:22 AM

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...

#4 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,377 posts
  • LocationCheshire, UK

Posted 12 February 2013 - 04:41 AM

did you pass it a username and password in the query string?

moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#5 PHP_CHILD

PHP_CHILD

    Advanced Member

  • Members
  • PipPipPip
  • 30 posts

Posted 12 February 2013 - 08:24 AM

did you pass it a username and password in the query string?

yes i did.. thanks i sorted it out...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com