samJL
samJL

Reputation: 737

jquery .post not working

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?

Upvotes: 0

Views: 146

Answers (2)

Jack
Jack

Reputation: 1376

Try using JQuery AJAX Instead:

$.ajax({
   type: "POST",
   url: "action.php",
   data: "passtext=Hello"
});

Upvotes: 1

RabidFire
RabidFire

Reputation: 6330

You need to add this line at the beginning of your files before you deal with sessions:

session_start();

From the PHP docs:

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

Upvotes: 2

Related Questions