AJAX无法正确发送会话变量
我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论