php - Return value from ajax form -
i have login form in php. have used ajaxform page not reloaded when username or password wrong. have page checklogin.php checks whether username , pw there in database , returns count of number of rows. want compare count in login.php , display error message if count=1 , redirect page if count=2. tried display count in errordiv using target: 'errordiv' , checking innerhtml failed so.
my login.php
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script> <script type="text/javascript" src="scripts/jquery.form.js"></script> <!--slider-in icons--> <script type="text/javascript"> function checklogin() { var request=$("#login-form").ajaxform( { target:'#errordiv' }).abort(); request.submit(); var divmsg=document.getelementbyid("errordiv"); if (divmsg.childnodes[0].nodevalue == "1") { divmsg.childnodesp[0].nodevalue="sorry"; alert ("invalid username or password");} //i didn't alert divmsg content didn't change }; </script> </head> <body> <!--wrapper--> <div id="wrapper"> <!--login form--> <form name="login-form" id="login-form" class="login-form" action="checklogin.php" method="post"> <!--content--> <div class="content"> <!--username--><input name="un" type="text" class="input username" placeholder="username" onfocus="this.value=''" required="required" /><!--end username--> <!--password--><input name="pw" type="password" class="input password" placeholder="password" onfocus="this.value=''" required="required" /><!--end password--> </div> <!--end content--> <!--footer--> <div class="footer"> <!--login button--><input type="submit" name="submit" value="login" class="button" onclick="checklogin()" /><!--end login button--> </div> <!--end footer--> </form> <div id="errordiv" class="errordiv"></div> <!--end login form-->
checklogin.php
<?php include ('dbconn.php'); $user = trim($_post['un']); $pass = trim($_post['pw']); $user = mysql_real_escape_string($user); $pass = mysql_real_escape_string($pass); $result=mysql_query("select id login username='$user' , password='$pass'") or die (mysql_error()); $result= mysql_fetch_assoc($result); $num= count ($result); echo $num; ?>
how can value of $num in login.php without displaying div , compare value of $num , accordingly display errormsg in errordiv or if $num==2 redirect page.
change code include code change div , provide alert included in callback.
var request=$("#login-form").ajaxform( { target:'#errordiv', success: function (html) { if ($("#errordiv).html() == "1") { $("#errordiv).html("sorry"); alert ("invalid username or password"); } } }).abort(); request.submit();
Comments
Post a Comment