AJAX无法正确发送会话变量

发布于 2025-01-25 07:36:14 字数 2310 浏览 4 评论 0原文

我是Ajax和PHP的新手,目前正在一个小网站上工作,在该网站上我将PHP对象初始化为会话变量。这些功能和对象都可以在PHP中可行,并用SWIG编译。当我在调用Ajax后尝试访问这些变量时,我发送的整数工作正常。但是,我发送的对象设置为0。下面是我的代码,对任何建议或帮助都非常感谢。谢谢你!

以下是显示网站的代码:index.php

<?php
session_start();
$_SESSION['w_id'] = 1;
$_SESSION['wordSet'] = init_WordSet(2234);

?>

<html>
        <head>   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><head>

        <body>
                <form method="get" class="ajax">
                        <input type="text" name="w_str">
                        <input type="submit">
                </form>
                <script>
                        $('form.ajax').on('submit', function(e){
                                e.preventDefault();
                                //Issue: How to get the input from php?
                                $.ajax({
                                        type: "GET",
                                        url: 'includes/functions.php',
                                        data: ({w_str: 'data'}),
                                        success: function(result){
                                                alert(result);
                                        }

                                });

                        });
                </script>




        </body>


</html>

的PHP代码

<?php

        session_start();
        echo $_SESSION['w_id'] . ' ' . $_SESSION['wordSet'];
?>

以下是Ajax调用用于初始化单词集的代码

struct WordSet* init_WordSet(int totalWords){
    //the total number of word blocks
    int arrLength = ceil((double)totalWords / (double)(sizeof(unsigned long) * NUM_BYTES));

    //the hash set that contains whether all words have been, or have not been used 
    struct WordSet *wordSet = malloc(sizeof(struct WordSet));
    wordSet->words = calloc(arrLength, sizeof(unsigned long)); 
    int i; 
    for(i = 0; i < arrLength; i++){
        //setting all the words to be unused
        wordSet->words[i] = 0; 
    }
    wordSet->totalWords = totalWords; 
    return wordSet; 
    
}```

I am very new to AJAX and PHP and am currently working on a small website where I initialize php objects as session variables. The functions and objects are all workable in php and compiled with SWIG. When I try to access these variables after calling AJAX, the integer that I send over works well. However, the object that I send over gets set to 0. Below is my code, any advice or help is greatly appreciated. Thank you!

Below is the code that displays the website: index.php

<?php
session_start();
$_SESSION['w_id'] = 1;
$_SESSION['wordSet'] = init_WordSet(2234);

?>

<html>
        <head>   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><head>

        <body>
                <form method="get" class="ajax">
                        <input type="text" name="w_str">
                        <input type="submit">
                </form>
                <script>
                        $('form.ajax').on('submit', function(e){
                                e.preventDefault();
                                //Issue: How to get the input from php?
                                $.ajax({
                                        type: "GET",
                                        url: 'includes/functions.php',
                                        data: ({w_str: 'data'}),
                                        success: function(result){
                                                alert(result);
                                        }

                                });

                        });
                </script>




        </body>


</html>

Below is the php code that Ajax calls

<?php

        session_start();
        echo $_SESSION['w_id'] . ' ' . $_SESSION['wordSet'];
?>

The code for initializing the word set

struct WordSet* init_WordSet(int totalWords){
    //the total number of word blocks
    int arrLength = ceil((double)totalWords / (double)(sizeof(unsigned long) * NUM_BYTES));

    //the hash set that contains whether all words have been, or have not been used 
    struct WordSet *wordSet = malloc(sizeof(struct WordSet));
    wordSet->words = calloc(arrLength, sizeof(unsigned long)); 
    int i; 
    for(i = 0; i < arrLength; i++){
        //setting all the words to be unused
        wordSet->words[i] = 0; 
    }
    wordSet->totalWords = totalWords; 
    return wordSet; 
    
}```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文