Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更新Javascript中的会话变量_Javascript_Php_Jquery_Ajax_Session Variables - Fatal编程技术网

更新Javascript中的会话变量

更新Javascript中的会话变量,javascript,php,jquery,ajax,session-variables,Javascript,Php,Jquery,Ajax,Session Variables,我有一个名为index.php的页面,其中有我所有的php函数、Java脚本和html标记。 在点击按钮(添加到购物车)事件后,我想调用一个php函数(AddToCart($itemID))来更新会话变量。你能告诉我如何在我的代码中实现这一点吗 <?php session_start(); $_SESSION['Mid']=""; $_SESSION['$UserName']="test"; ?> //html part <button id='cart' onclick

我有一个名为index.php的页面,其中有我所有的php函数、Java脚本和html标记。 在点击按钮(添加到购物车)事件后,我想调用一个php函数(AddToCart($itemID))来更新会话变量。你能告诉我如何在我的代码中实现这一点吗

<?php
session_start();
$_SESSION['Mid']=""; 
$_SESSION['$UserName']="test"; ?>

//html part
<button id='cart' 
onclick="<script>idk how to call my function here</script>"
class="w3-button">ADD TO CART</button>

 //my php function
<?php
  function addToCart($Mid){
    if(!isset($_SESSION['$UserName'])){
            header('Location:signin.html');
    }
    $_SESSION['Mid'].="+".$Mid;
    echo "<script type='text/javascript'>alert(\"ADDED TO CART\");</script>";
  }
?>

//html部分
添加到购物车
//我的php函数

使用aJax调用调用另一个更新它的PHP脚本:

        var SendInfo= {     "your": data,
                            "your1": data1 };


        $.ajax({
                type: 'POST',
                url: 'https://yourscript.com/updatesession.php',
                data: SendInfo,
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                traditional: true,
                success: function (data) {
                    //dostuff
                }
        });

使用aJax,javascript不能直接影响会话变量。您可以在javascript中使用此
var javaScriptVariable=