I'm trying a simple if/else statement with Javascript & PHP (with jQuery cookie plugin).
My problem: every time this script fires, it executes both IF and ELSE statements. Even when my cookie is set, it creates a new one and adds it into my database. Am I using both languages properly?
Here's what it looks like:
<script type="text/javascript">
//check to see if cookie is present
if ($.cookie('uid')) {
var userCookie = $.cookie('uid');
alert(userCookie);
}
else {
//create cookie and check to see if it is taken
<?php
$success = false;
while (!$success) {
$cookie = createUniqueCookie();
$success = checkIfCookieIsTaken($cookie);
}
//store into database and set into browser
if ($result = $mysqli->query("INSERT INTO users (cookie) VALUES ('" . $cookie . "')")) {
?>
$.cookie('uid', '<?php echo $cookie ?>', { expires: 730, path: '/' });
var userCookie = $.cookie('uid');
alert('new cookie for user is '+ userCookie);
<?php
}
?>
}