javascript - Ajax for login check not working and submitting form -


i know there many answers problem i'm new ajax , javascript point me problem causing script fail.
made login check ajax based on answer check if user logged in on ajax page change . when submit button clicked function checks if user logged in or not. want display alert if not , proceed next page if is.
javascript:

    $('#bookform').submit(function(){     $.ajax({             url : '<?=base_url?>user/logged.php',             success : function(response){                 if (response.logged == true) {                     return true;                 }else{                     alert('you need log in');                     //e.preventdefault();                     return false;                 }             }     }); });   

my php script checks if user logged:

$response = array();  if(!isset($_session['userid'])) {      $response['logged'] = false;   }else{      $response['logged'] = true;  }  $response = json_encode($response);  echo($response); 

i'm new javascript/ajax , i'm not sure problem is. function returns alert not logged in user despite user being logged in.

you have add option : datatype:'json' in ajax call.

and echo json_encode($response); without return.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

unity3d - Fatal error- Monodevelop-Unity failed to start -