如何使用jQuery AJAX $ .post的存储PHP $ _SESSION变量?如何使用、变量、jQuery、post

2023-09-10 13:27:10 作者:哪吒么么哒i

帮助!

我在遇到麻烦摔跤AJAX为我工作。我有一个分页画廊每个图像下方的复选框,我需要存储在会话变量中的复选框的值,如果页面之间的用户移动,所以当他们在任何时间提交表单,将包括所有在所有页选定值。

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.

我使用这个jQuery的code:

I'm using this jQuery code:

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

和表单数据holder.php文件中这样说:

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)如何获得该复选框值了序列化()函数?我认为有更多的我必须做的东西,如值[]来获得该数组出来,然后我猜存储每一个作为一个单独的会话变量 - ?除非我可以存储数组作为$ _SESSION变量

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)之前,我甚至乱用任何的是,我补充说,行$ _SESSION ['保存'] =真; PHP脚本,然后我赞同我的画廊页的$ _SESSION键和值,看看是否AJAX请求甚至工作。不是。这$ _SESSION ['保存']不添加到回荡$ _SESSION变量的列表,当我返回页面。

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.

任何帮助将是很大的AP preciated !!

Any help would be greatly appreciated!!

推荐答案

您需要调用session_start()在你的表单数据holder.php文件。

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

每当你做出Ajax调用,你是从那个不知道任何在原页面设置变量的服务器请求一个新的/新的一页。

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.