Tuesday, 15 January 2013

Return user id along with Ajax Success response -



Return user id along with Ajax Success response -

i have ajax login submit works fine. need send $user_id login page on success. cant figure out how.

below have.

this php page

<? if (!securepage($_server['php_self'])){die();} //prevent user visiting logged in page if he/she logged in if(isuserloggedin()) { header("location: account.php"); die(); } //forms posted if(!empty($_post)) { $errors = array(); $username = sanitize(trim($_post["user"])); $password = trim($_post["password"]); //perform validation //feel free edit / alter required if($username == "") { $response['success'] = false; } if($password == "") { $response['success'] = false; } if(count($errors) == 0) { //a security note here, never tell user credential wrong if(!usernameexists($username)) { $response['success'] = false; } else { $userdetails = fetchuserdetails($username); //see if user's business relationship activated if($userdetails["active"]==0) { $response['success'] = false; } else { //hash password , utilize salt database compare password. $entered_pass = generatehash($password,$userdetails["password"]); if($entered_pass != $userdetails["password"]) { //again, know password @ fault here, lets not give away combination incase of bruteforcing $response['success'] = false; } else { //passwords match! we're go' $response['success'] = true; } } } } } //$user_id = $loggedinuser->user_id; echo json_encode($response); ?>

here ajax calls php page. , need retrieve id php page.

<script type="text/javascript"> //login ajax send on user , pass ls function handlelogin() { var form = $("#loginform"); //disable button can't resubmit while wait $("#submitbutton",form).attr("disabled","disabled"); var e = $("#user", form).val(); var p = $("#password", form).val(); console.log("click"); if(e != "" && p != "") { var str = form.serialize(); //mcdon(str); $.ajax({ type: 'post', url: 'http://vsag.actualizevps.com/loginmobile.php', crossdomain: true, data: {user: e, password :p}, datatype: 'json', async: false, success: function (response){ //alert ("response"); if (response.success) { //alert("you're logged in"); window.localstorage["user"] = e; //window.localstorage["password"] = md5(p); //window.localstorage["uid"] = data.uid; window.location = "create.html"; } else { alert("your login failed"); //window.location("main.html"); location.reload(); } }, error: function(error){ //alert(response.success); alert('could not connect database' + error); window.location = "main.html"; } }); } else { //if email , password empty alert("you must come in user , password"); } homecoming false; } </script>

the $response value true or false @ moment, homecoming array:

$response = array("success" => true, "userid" => $user_id);

and on ajax response, response variable

response.userid

will contain user id

ajax

No comments:

Post a Comment