javascript - AJAX is running error function instead of success function -
i trying database login work. ajax call keeps running error function instead of success, , struggling understand why.
function dologin() { var formdata = convertformtojson("#login-form"); console.log('login data send: ', formdata); $.ajax({ url: 'php/login-session.php', type: 'post', datatype: 'json', data: formdata, success: function(logindata) { console.log('login data returned: ', logindata); var status = logindata['status']; if(status == "fail"){ $("#loginerror").html(logindata['msg']); $("#loginerror").cs('display', 'block'); } else { getuserprofileinfo(); $("login-form").trigger('reset'); } }, error: function(jqxhr, textstatus, errorthrown){ console.log(jqxhr.statustext, textstatus); } }); }
the console logs:
login data send: object {loginusername: "test", loginpassword: "test"}
ok parsererror
my login-session.php code is:
<?php if (session_status() == php_session_none) { session_start(); } $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'root'; $dbname = 'anamadeus'; $methodtype = $_server['request_method']; $data = array('status' => 'fail', 'msg' => '$methodtype'); if($methodtype === 'post'){ //check ajax if(isset($_server['http_x_requested_with']) && strtolower($_server['http_x_requested_with']) == 'xmlhttprequest') { if(isset($_post['loginusername']) && !empty($_post['loginusername']) && isset($_post["loginpassword"]) && !empty($_post["loginpassword"])){ // data post , store in variables $username = $_post["loginusername"]; $password = $_post["loginpassword"]; try { $conn = new pdo('mysql:host=$dbhost;dbname=$dbname', $dbuser, $dbpass); $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception); $sql = 'select * user username = :log , password = :pwd'; $statement = $conn->prepare($sql); $statement->execute(array(":log" => $username, ":pwd" => $password)); // should 1 if there's user user value , password value $count = $statement->rowcount(); if($count > 0){ $rows = $statement->fetchall(pdo::fetch_assoc); $returnedusername = $rows[0]['username']; $returnedpassword = $rows[0]['password']; $_session['username'] = $returnedusername; $_session['loggedin'] = true; $sid=session_id(); $data = array("status" => "success", "sid" => $sid); } else { $data = array("status" => "fail", "msg" => "user name and/or password not correct."); } } catch(pdoexception $e) { $data = array("status" => "fail", "msg" => $e->getmessage()); } } else { $data = array("status" => "fail", "msg" => "either login or password absent."); } } else { // not ajax $data = array("status" => "fail", "msg" => "has ajax call."); } } else { // simple error message, taking post requests $data = array("status" => "fail", "msg" => "error: post allowed."); } echo json_encode($data, json_force_object); ?>
please me.
i running database , php on mamp
epascarello had correct answer, had change single quotes double quotes.
Comments
Post a Comment