我想存储在PHP数组或PHP会话中的值由ajax在jquery我发送一些值由ajax在一个php页面上,并希望存储它们
问题:每次数组/会话返回最新发送的值,而不是我之前发送的值
我希望以前发送的值应该保留在数组或会话中
我的代码如下
Js文件编码
$.ajax({
url: "http://domain.com/ajax.php",
type:"POST",
data: { name : nname ,
clas : nclass ,
rows : nrows ,
cols : ncols ,
types : ntype ,
check : ncheck ,
count : ncldiv
},
success: function(data){
alert(data);
}
});PHP文件
<?php
session_start();
$_SESSION['feilds'] = array();
$type = $_POST['types'];
$name = $_POST['name'];
$class = $_POST['clas'];
$rows = $_POST['rows'];
$cols = $_POST['cols'];
$check = $_POST['check'];
$count = $_POST['count'];
$output_string = array('TYPE'=>$type,'NAME'=>$name,'CLASS'=>$class,'ROWS'=>$rows,'COLS'=>$cols,'REQUIRED'=>$check);
array_push($_SESSION['feilds'] , $output_string );
print_r($_SESSION['feilds']);
?>发布于 2013-06-28 15:03:55
问题是您总是将$_SESSION['feilds']变量实例化为空数组。使用以下命令:
<?php
session_start();
if (!isset($_SESSION['feilds'])) {
$_SESSION['feilds'] = array();
}
$type = $_POST['types'];
$name = $_POST['name'];
$class = $_POST['clas'];
$rows = $_POST['rows'];
$cols = $_POST['cols'];
$check = $_POST['check'];
$count = $_POST['count'];
$output_string = array('TYPE'=>$type,'NAME'=>$name,'CLASS'=>$class,'ROWS'=>$rows,'COLS'=>$cols,'REQUIRED'=>$check);
array_push($_SESSION['feilds'] , $output_string );
print_r($_SESSION['feilds']);
?>发布于 2013-06-28 15:02:51
这要归功于$_SESSION['feilds'] = array();。
当页面被调用$_SESSION['feilds']时,赋值为空的array.That就是为什么你只能得到当前值的原因。
使用isset或empty检查$_SESSION['feilds']是否已存在
if(empty( $_SESSION['feilds'] )) {
$_SESSION['feilds'] = array();
}发布于 2013-06-28 15:29:42
you wrote $_SESSION['feilds'] = array();
Every time it assign a empty array at session so it will wash out previous data.
if you want to retain your previous data then first add check like
if(empty( $_SESSION['feilds'] )) {
$_SESSION['feilds'] = array();
}
after that you assign value to session as you are doing
hope it will help you :)https://stackoverflow.com/questions/17358968
复制相似问题