I'm trying to use jquery mobile to create a simple web app. At one point in the app, I need to get information from a php page to a javascript page, and then back and forth again (i.e., javascript will update the cookie, which will be read by php, which will then be read by javascript, etc.).
In order to test that I could set a cookie with php and then read it with javascript on the same page load, I created the following, working script:
<?php
include("../../../connectFiles/connect_dev.php");
setcookie("cookie_test","hello", 0, '/');
?>
<html>
<head><title>Cookie setting test</title></head>
<body>
<script type="text/javascript">
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
alert(readCookie('cookie_test'));
</script>
Hello there, I hope you were able to see my cookie!
</body>
As I mentioned, this worked just fine. However, when I take the same code and try and implement it inside of a jquery mobile framework, I can't seem to find the cookie. It keeps coming up as "null." I'm guessing this has something to do with when the cookie is being accessed through the jquery mobile framework. I've bound the readCookie function to the pageshow function in jquery mobile like this:
$(document).on('pageshow',function(e, ui){
if (e.target.id == 'assessment') {
var objective = readCookie('objective');
alert("objective: "+objective);
Unfortunately, this results in a "null" cookie value every time. What do I need to do to be able to set the cookie in PHP and call it in javascript?