PHP_CHILD Posted February 11, 2013 Share Posted February 11, 2013 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... Quote Link to comment https://forums.phpfreaks.com/topic/274339-php-basic-ajax-example-not-returning-0-value-from-php-file/ Share on other sites More sharing options...
Barand Posted February 11, 2013 Share Posted February 11, 2013 When you execute the php directly in your browser instead of via AJAX, what do you get? Quote Link to comment https://forums.phpfreaks.com/topic/274339-php-basic-ajax-example-not-returning-0-value-from-php-file/#findComment-1411743 Share on other sites More sharing options...
PHP_CHILD Posted February 12, 2013 Author Share Posted February 12, 2013 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... Quote Link to comment https://forums.phpfreaks.com/topic/274339-php-basic-ajax-example-not-returning-0-value-from-php-file/#findComment-1411906 Share on other sites More sharing options...
Barand Posted February 12, 2013 Share Posted February 12, 2013 did you pass it a username and password in the query string? Quote Link to comment https://forums.phpfreaks.com/topic/274339-php-basic-ajax-example-not-returning-0-value-from-php-file/#findComment-1411912 Share on other sites More sharing options...
PHP_CHILD Posted February 12, 2013 Author Share Posted February 12, 2013 did you pass it a username and password in the query string? yes i did.. thanks i sorted it out... Quote Link to comment https://forums.phpfreaks.com/topic/274339-php-basic-ajax-example-not-returning-0-value-from-php-file/#findComment-1411949 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.