简体   繁体   English

如何使用 AJAX 更改 PHP SESSION 变量?

[英]How to change a PHP SESSION VARIABLE using AJAX?

I am very new to PHP and just starting to learn AJAX.我对 PHP 非常陌生,刚刚开始学习 AJAX。

I want to know if its possible to change a PHP SESSION VARIABLE using AJAX and how to do it... I tried to check on some examples but most of it are just displaying some values on the browser.我想知道是否可以使用 AJAX 更改 PHP SESSION VARIABLE 以及如何做到这一点......我试图检查一些示例,但大多数只是在浏览器上显示一些值。

Example: http://www.w3schools.com/php/php_ajax_php.asp示例: http://www.w3schools.com/php/php_ajax_php.asp

index.php : index.php

<?php
 $_SESSION['test'] = 1;
?>

<script type="text/javascript" src="functions.js" > </script>

<div>
 <?php echo "Before: " . $_SESSION['test']; ?>
 <input type="submit" value="CLICK" onclick="resetVar('<?php echo $_SESSION['test']; ?>')" />
 <?php echo "After: " . $_SESSION['test']; ?>
</div>


functions.js : functions.js

function resetVar(sessionVal) {
 sessionVal = null;

 var httpRequest;

 if(window.XMLHttpRequest)  httpRequest = new XMLHttpRequest();
 if(window.ActiveXObject) httpRequest = new ActiveXObject("Microsoft.XMLHTTP");

 if(httpRequest.readyState == 4 && httpRequest.status == 200) {
  // I don't know what to put here...
 }

 httpRequest.open("get", "index.php?$_SESSION['test'] = " + sessionVal, true);
 httpRequest.send();
}

I know its terribly wrong and I need help to fix this.我知道它非常错误,我需要帮助来解决这个问题。 I want AJAX to do a similar unset the $_SESSION['test'] variable when I hit the submit button.当我点击提交按钮时,我希望 AJAX 执行类似的取消设置$_SESSION['test']变量。 I want it to do someting like unset($_SESSION['test']) .我希望它做一些类似unset($_SESSION['test'])事情。 Please help me with this...请在这件事上给予我帮助...

Practically speaking your script does nothing in many ways, here and there.实际上,您的脚本在很多方面都没有做任何事情,在这里和那里。 Seems to me you don't understand the main and basic concepts of programming for the web.在我看来,您不了解 web 编程的主要和基本概念。

Please read and understand the basics: http://www.google.com/search?q=how+php+works请阅读并理解基础知识: http://www.google.com/search?q=how+php+works

Else there isn't a thing I can help with but doing the whole task for you.否则我无能为力,只能为您完成整个任务。

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

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