简体   繁体   English

jQuery cookie-如何使用cookie

[英]Jquery cookie-how to use cookie

I need to post ID value from page with jeasyui Grid, to another demo.php,for creating pdf report. 我需要将带有jeasyui Grid的页面的ID值发布到另一个demo.php,以创建pdf报告。 So my idea was to use cookies, like this. 所以我的想法是像这样使用cookie。

  $(function () {

        $("#btnCookie").bind("click", function () {
        var row = $('#tt').datagrid('getSelected');
    if (row){
            $.cookie("name",row.id);
          alert($.cookie("id"));//just to check for value ,and it is OK!
            window.location.href = "demo.php";
        }});
    });

after submit,I get right value. 提交后,我得到正确的价值。 On demo.php I use this to get cookies value: 在demo.php上,我使用它来获取cookie值:

         $(function () {
        if ($.cookie("id") != null ) {
           var id = $.cookie("name");

            $.removeCookie("id");
           }
    });
</script>

now my problem is to put cookie values in next query in php section: 现在我的问题是将cookie值放在php部分的下一个查询中:

$result = mysqli_query($con,"SELECT * FROM stan where id="HERE I need cookie value"

What you're looking for is: 您正在寻找的是:

$_COOKIE["something"] 

I hope you find out the rest :) (Make sure you use proper quotes in your string, not like you wrote it in the question; also be aware of the syntax of implanting array element refs into strings; finally, watch out for cookie value, it can be manipulated so protect your query against tricky values.) 我希望您能找到其余的内容:)(确保在字符串中使用正确的引号,而不是像在问题中所写的那样;还请注意将数组元素ref植入字符串中的语法;最后,请注意cookie值) ,因此可以对其进行操作,从而保护您的查询免于棘手的值。)

You're setting jQuery cookie correctly. 您正在正确设置jQuery cookie。 To call it and search your database just use it in the query like so: 要调用它并搜索数据库,只需在查询中使用它,如下所示:

<?  
    $value = $_POST['rowValue'];

    $stmt = $con->prepare("
        SELECT * 
        FROM stan 
        WHERE id = ?
    ");

    $stmt->execute(array($value));

    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);   
?>

EDIT: Updated because of comment: 编辑:由于评论更新:

<script>
$.post("demo.php", {
        rowValue: row.id //The cookie value
    }
});
</script>

So I now understand that you want to pass the cookie value, not the cookie name to your php file. 因此,我现在知道您想将cookie值而不是cookie名称传递给您的php文件。 For this you don't need to set a cookie, you can just send the value to the php file. 为此,您无需设置Cookie,只需将值发送到php文件即可。 In my example I've used the jQuery ajax post function. 在我的示例中,我使用了jQuery ajax post函数。 I've updated my example PHP accordingly. 我已经相应地更新了示例PHP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM