【问题标题】:How can pass my php session_id() in ajax jQuery如何在 ajax jQuery 中传递我的 php session_id()
【发布时间】:2018-07-27 09:45:43
【问题描述】:
// Show Cart Quantity
function showCartById(){
    var inProcess = false;//Just to make sure that the last ajax call is not in process
    if (inProcess) {
        return false;//Another request is active, decline timer call ...
    }
    inProcess = true;//make it burn ;)
    var sId = $(this).attr('sid');
    var data = 'sId='+sId;
    jQuery.ajax({
        url: 'cart.php', //Define your script url here ...
        data: data, //Pass some data if you need to
        method: 'POST', //Makes sense only if you passing data
        success: function(answer) {
            alert(answer)
            inProcess = false;//Queue is free, guys ;)
        },
        error: function() {
            //unknown error occorupted
            inProcess = false;//Queue is free, guys ;)
        }
    });

}

我想通过 ajax 请求方法传递我的 PHP Session_id()。但如果没有 Session_id() 它工作得很好。

【问题讨论】:

  • 您使用的是负载平衡服务器吗?如果没有,那么服务器已经知道你的 session id,你不需要传递它
  • 尝试在数据中使用 JSON 格式而不是 sId=sId,例如..... { "sId" : sessionId}
  • 如果您想在 cart.php 中发送会话值,只需在 cart.php 顶部使用 session_start(),您设置的所有会话都将可用。

标签: php jquery sql ajax


【解决方案1】:

你可以做var session_id = '<?php echo session_id();?>';并将其传递给数据

【讨论】:

  • public function getTotalQue($sId){ $sId = mysqli_real_escape_string($this->db->link, $sId); $query = "SELECT quantity FROM tbl_cart WHERE sId = '$sId'"; $result = $this->db->select($query); $qty = 0; if ($result) { while ($data = $result->fetch_assoc()) { $qty = $qty+$data['quantity']; } echo $qty; }else{ echo "0"; } }
  • 好的,你可以做 var session_id = '';并将其传递给数据参数
  • 感谢您帮助我。我已经解决了这个问题。
  • 它有帮助还是你找到了一些不同的解决方案?
  • 是的,我用过header中的样子。<script type="text/javascript"> var sId = '<?php echo session_id();?>'; </script>
猜你喜欢
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
  • 2012-06-12
  • 2015-08-21
  • 1970-01-01
  • 2015-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多