javascript method onclick is executing even submit button is

Im new to php and javascript so please bear with me.index.php:<?php<br /> $_SESSION[‘test’] = 1;?> <?php echo “Before: ” . $_SESSION[‘test’]; ?> ;” /> <?php echo “After: ” . $_SESSION[‘test’]; ?> Why is it that $_SESSION[‘test’] is already 0 when I haven’t clicked the button yet??? Please help me……………………

via PHP Website Development » Search Results » ajax:

javascript method onclick is executing even submit button is

Im new to php and javascript so please bear with me.
index.php:
$_SESSION[‘test’] = 1;
?>


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

For more info: javascript method onclick is executing even submit button is

PHP Website Development » Search Results » ajax

javascript method onclick is executing even submit button is

Share this post:

Related Posts

Leave a Comment