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