Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

Error with cookie that stores a javascript variable to be read with php

I been working in a php/javascript quiz, and in order to store a variable located in a script that is Quiz.score, I have created a cookie that it purpose is, when test ended and score is shown, it would save the data to read it in php and send it to a mysql server's table, the cookie's structure is the following:

createCookie.js

// Creating a cookie after the document is ready 
$(document).ready(function() {
  createCookie("scr", Quiz.score, "10");
});
// Function to create the cookie 
function createCookie(name, value, days) {
  var expires;
  if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    expires = "; expires=" + date.toGMTString();
  } else {
    expires = "";
  }
  document.cookie = escape(name) + "=" +
    escape(value) + expires + "; path=/";
}

But when I try to request for the cookie by specifying the name, in the following code:

rvnqended.php

<?php 
    echo $_COOKIE["scr"]; 
?> 
<script src="js/createCookie.js"></script>

It gives me the following error:

Notice: Undefined index: scr in C:xampphtdocsuttn vnqended.php on line 2

Also when I use the console it shows the following error: createCookie.js:2 Uncaught ReferenceError: $ is not defined at createCookie.js:2

In sources shows the following text: Uncaught ReferenceError: $ is not defined

What am I doing wrong?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...