Im new to php and javascript so please bear with me.
index.php
:
<?php
$_SESSION['test'] = 1;
?>
<div>
<?php echo "Before: " . $_SESSION['test']; ?>
<input type="submit" value="CLICK" onclick="<?php $_SESSION['test'] = 0; ?>;" />
<?php echo "After: " . $_SESSION开发者_开发问答['test']; ?>
</div>
Why is it that $_SESSION['test']
is already 0
when I haven't clicked the button yet??? Please help me...
PHP is a preprocessor. Everything you write in PHP is executed BEFORE the page is presented, while javascript executes clientside as the page is running. Therefore, you cannot set a PHP value with a javascript event.
PHP is a server-side language; it is parsed and run before anything is even sent to the browser. It does not interact with JavaScript.
You will need to use AJAX to call the php set the session on click via javascript. I suggest having a look at XMLHTTPRequest: http://www.w3.org/TR/XMLHttpRequest/ , or if you don't want to read all of that and learn it, I suggest looking at a javascript library such as http://www.jquery.com, which should simplify what you need to do.
You need to call session_start()
before anything can be stored in the session.
The way you are trying to do it is impossible.
Use AJAX
精彩评论