我正在嘗試使用Javascript的PHP(與jQuery cookie plugin)一個簡單的if/else語句。執行兩個if/else語句的Javascript和PHP
我的問題:每次腳本觸發時,它都會執行IF和ELSE語句。即使設置了我的cookie,它也會創建一個新的並將其添加到我的數據庫中。我是否正確使用兩種語言?
下面是它的樣子:
<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
}
?>
}