passing variable back from php to jquery? -


i trying convert login page use html , use jquery , php fetch , process results, reason if want go mobile project there.

the problem have passing variables php jquery display @ same time.

my example index.html

<!doctype html> <html> <head>     <meta name="viewport" content="width=device-width, initial-scale=1">         <meta http-equiv="content-type" content="text/html; charset=utf-8">     <title>auth demo 2</title>     <script src="jquery/jquery-1.7.2.min.js"></script>     <script src="main.js"></script> </head>  <body onload="handlelogin()">          <form id="loginform">              <label for="email">email:</label>             <input type="text" name="email" id="email" value="" placeholder="email" />                <label for="password">password:</label>             <input type="password" name="password" id="password" value="" placeholder="password" />           <input type="submit" value="login" id="submitbutton">         </form>  </body> </html>  

main.js

    function handlelogin(){          var form = $("#loginform");         var u = $("#email", form).val();         var p = $("#password", form).val();          if(u!= '' && p!= '') {              $.post("http://www.mysite.co.uk/login.php",{                          email:$('#email', form).val(),                         password:$('#password', form).val(),                         rand:math.random()                        } ,function(data)                           {                              if(data=='yes') //if correct login detail                              {                                  alert( "request success: ");                              }                              else                              {                                     //add reason in alert box here                                   alert ("failed reason")                               }                    });         } else {                     alert( "username/password empty");                }          return false;//not post  form physically       } 

login.php

<?//get posted values require_once("../backend/functions.php"); dbconn(true);   if ($_post["email"] && $_post["password"]) {     $password = passhash($_post["password"]);      if (!empty($_post["email"]) && !empty($_post["password"])) {         $res = sql_query_exec("select id, password, secret, status, enabled users email = " . sqlesc($_post["email"]) . "");         $row = mysql_fetch_assoc($res);          if ( ! $row || $row["password"] != $password )             $message = "wrong password";         elseif ($row["status"] == "pending")             $message = "account pending";         elseif ($row["enabled"] == "no")             $message = "account suspened";     } else         $message = "no username/password added";      if (!$message){         logincookie($row["id"], $row["password"], $row["secret"]);         if (!empty($_post["returnto"])) {             header("refresh: 0; url=" . $_post["returnto"]);             die();         }         else {             echo  "yes";             die();         }     }else{         echo $message;     } }  logoutcookie(); 

as can see when login fails have various reasons want pass alert box. whats best way go this

just alert data

  if(data=='yes') //if correct login detail   {       alert( "request success: ");   }    else{         alert (data)    } 

however recommmend response json.....

if(u!= '' && p!= '') {      $.post("http://www.mysite.co.uk/login.php",{                 email:$('#email', form).val(),                password:$('#password', form).val(),                rand:math.random()           } ,function(data)              {                 if(data=='yes') //if correct login detail                 {                     alert( "request success: ");                 }                 else                 {                                                      //add reason in alert box here                     alert (data)                  }            });    } else {          alert( "username/password empty");   }       return false;//not post  form physically   } 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -