views:

937

answers:

1

Help!

I'm having trouble wrestling AJAX to work for me. I have a paginated gallery with checkboxes beneath each image and I need to store the checkbox values in session variables if a user moves between the pages, so when they submit the form at any time it will include all checked values across all pages.

I'm using this jQuery code:

$(document).ready(function() {
    $(".gal-nav").click(function() {
     $.post("form-data-holder.php", $("#gallery-form").serialize());
    });
});

and the form-data-holder.php file says this:

<?php

    $_SESSION['saved'] = "true";

    foreach ($_POST as $key=>$value ) {
        if ( $key !== "submit" ) {
            $value = htmlentities(stripslashes(strip_tags($value)));
            $_SESSION[$key] = $value;
        }
    }

?>

I have two issues --

1) How do I get the checkbox values out of the serialize() function? I think there's more I have to do with something like value[] to get that array out, and then I guess store each one as a separate session variable -- unless I can store an array as a $_SESSION variable?

2) Before I even mess with any of that, I added that line $_SESSION['saved'] = "true"; to the php script and then I echo the $_SESSION keys and values on my gallery page to see if the AJAX request is even working. It's not. That $_SESSION['saved'] isn't added to the list of echoed $_SESSION variables when I return to the page.

Any help would be greatly appreciated!!

+2  A: 

You need to call session_start() in your form-data-holder.php file.

Every time you make the ajax call, you are requesting a new / fresh page from the server that isn't aware of any of the variables set in the original page.

jeroen
Sorry, the session has already been started by the fact that the user must be logged in to see the gallery at all.
Jason Rhodes
Yes, but when you make an ajax call, it's loading a new php page, starting fresh, so you will need to call it there as well.
jeroen
Hmm ok I added <?php session_start(); ?> to the top of the form-data-holder.php file -- no change. Do I have to associate that session with the other session somehow?
Jason Rhodes
Not normally, if you're not starting the session in a special way with a session name or anything. Are you sure form-data-holder.php is opened / found?
jeroen
No, not sure -- that seems to be the problem. But I've tried writing out the whole "http://..." address and it still doesn't work. Does it need to be a path?
Jason Rhodes
Can you access you error log to check if it gives a file-not-found error?
jeroen
I've actually never done that so I'm not sure.I finally got it working though, so it's seeing the file -- however I'm now getting a lot of NOTICE: Undefined index for any of the $_POST variables that were left blank. I assume that's not a problem once I turn the error reporting off. I'm also still unable to access the checkbox array variables so I won't be able to set the session yet.
Jason Rhodes
I'd personally add the necessary checks (if (isset(...))) instead of turning error reporting off. What is the problem with the session exactly?
jeroen
Well I have to turn error reporting off to go live -- I don't want PHP error messages going live to end users. But yes, I will add the isset checks for better code. And as for the checkbox values, I've just never quite understood how to get the actual values out of the multidimensional array -- where they are and how deep, how to call them, etc. It's confusing each time I do it.
Jason Rhodes