I am working with the following code:
action.php
<?php
$_SESSION['passtext'] = $_POST['passtext'];
?>
index.php
<script type="text/javascript">
$(function(){
$("#clickable").click(function() {
$.post("action.php", { passtext: "Hello" } );
alert('refresh the page to see the session text');
});
});
</script>
<a href="javascript:void(0);" id="clickable">click me to set session text</a>
<?php if(isset($_SESSION['passtext'])) echo $_SESSION['passtext']; ?>
When I click the link, I get the alert, but it seems that action.php isn’t running at all — because I will refresh the page and I see no echo of $_SESSION[‘passtext’] (it isn’t being set)
There are no errors in the Error Console either
What am I missing?
You need to add this line at the beginning of your files before you deal with sessions:
From the PHP docs: