I’m writing some code to allow user to book a table in a restaurant. But my client asked me to allow user choose from the restaurant menu some items and then come back and reserve the table. If user don’t want to choose menu then they may just ignore the step.
To do so I just printed a message “Do you want to choose menu?” and then a hyperlink to the menu page. I used iavascript onClick event to listen whether user clicked or not and if clicked I called a function setsession to set my session to track my user’s activity in my website. I declared my function on top of the page.like-
<script>
function setsession(){
<?php $_session['reservation']= TRUE;
}
</script>
<form action="" method="post">
<p>Do you want to choose menu? Then <a href="somepage.php" onClick="setsession()">Click here.</a></p>
<p>rest of my code</p>
<p>confirm to reserve.</p>
<a href="javascript:{}" onclick="return Validate();submitform();">Confirm</a>
</form>
<?php if(isset($addnew) && !$error) ?>
<div class="success">
<p>sucess</p>
<?php $_SESSION['reservationprocess'] = FALSE;?>
</div>
<?php } ?>
everything worked fine. When I clicked on the hyperlink my session variable successfully. And when i clicked on confirm button (the hyperlink above) it successfully stores the data in database and pull the message success and unset the session.
but problem is I don’t how and why my session set again. How much I tried to unset my session but I failed. my rest of the code working well and there are no problem, as far I know. I’ve tested it. Is there any solution from the experts? Thanks in advance.
Sorry to disturb you again. But this time not to ask some novice question, but to answer my own question.
First of all I’m going to write my question in brief. I wanted to catch a event and wanted to use the event to store some value in my session to recognize that event in rest of my site unless the session is destroyed by somehow.
Al the variables used in .php file stores value in them and execute all operations when they are on the server. Browser displays the result. How can we expect while a user do something [in my case clicked] and it simply store a value in a php variable while there are no php variables inside the browser. So what can we do is simply store the result in a javascript variable or go back to server and set the value to the variable.
To call the server using ajax is a method of experts and honestly speaking I don’t know much of it. So I needed to look for something what i already know. Of course based on php. Finally I got the COOKIE.
To catch the event its obvious to use javascript. I used a event listener using jQuery and inside it I store a value in my browser’s cookie. and then used both javascript and php to check the cookie in my website.
This has a fallback. If user disables the cookie then you won’t have the option to use it.