Use logged in user information from API jQuery login response

Hello.

I have a login form that successfully checks credentials using ajax.

On the response, the object that obtains all of the user information is stored in the 'response' variable. I can log that response to the console without problem.

  1. How can I store the whole 'response' variable in SESSION, in order to use it across pages?
  1. Also, how can I alert (jQuery) or echo (php) a specific value from the response object (like the token, for example)?

JSFiddle : <-- CHANGE YOUR OWN API AND KEY TO MAKE IT WORK.

Hey Mauricio,

I believe I posted code that answered this type of question on Stackoverflow.com a few days back. Check out this link and let me know if this is what you were looking for (also please up vote the question on Stack while you're there).

http://stackoverflow.com/questions/31776550/get-user-info-from-knackhq-com-api-with-php?noredirect=1#comment51483485_31776550

You should also change your jquery code so that it's a basic AJAX call to your .php page with the code from the link (no need to put your ID's on the consumer end of your website). Something like this will do the trick:

<?php
session_start();
?>

(document).ready( function() { (’#login_form_box_ID’).on(‘submit’, function (event) {
event.preventDefault();

.ajax({ url: "PHP-PAGE-WITH-CODE-ABOVE.php", type: 'post', dataType: 'json', data: (’#login_form_box_ID’).serialize(),
success: function(users){
console.log(users);
alert(users);
}
}).done( function() {
location.reload();
})

})
})
</script>
</head>

<body>

<form id=“login_form_box_ID” class=“login_form_box_CSS” method=“post” action="">
<input id=“login_email_input_ID” class=“login_form_input_CSS” name=“login_input_EMAIL” value="" type=“text”/>
<input id=“login_password_input_ID” class=“login_form_input_CSS” type=“password” name=“login_input_PASSWORD” value=""/>
<input id=“login_form_submit_ID” class=“login_form_submit_CSS” type=“submit” value=“submit” name=“login_form_submit” />
</form>
</body>
</html>

I have all my website pages on .php as it’s easier with the API to pull the $_SESSION values across the site while the user is logged in.

Thanks,

Jon