通过jquery中的ajax在php数组/会话中存储值
我想通过jquery中的ajax将值存储在PHP数组或PHP会话中 我将通过ajax在php页面上发送一些值,并希望存储它们 问题:每次数组/会话都返回最新发送的值,而不是我之前发送的值通过jquery中的ajax在php数组/会话中存储值,php,jquery,ajax,Php,Jquery,Ajax,我想通过jquery中的ajax将值存储在PHP数组或PHP会话中 我将通过ajax在php页面上发送一些值,并希望存储它们 问题:每次数组/会话都返回最新发送的值,而不是我之前发送的值 我希望以前发送的值应该保留在数组或会话中 我的代码在下面 Js文件代码 $.ajax({ url: "http://domain.com/ajax.php", type:"POST", data: { name :
我希望以前发送的值应该保留在数组或会话中 我的代码在下面 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']);
?>
发生这种情况是由于$\u会话['feilds']=array()代码>
调用页面时,
$\u会话['feilds']
将分配空白数组。这就是为什么您只获取当前值
使用isset
或empty
if(empty( $_SESSION['feilds'] )) {
$_SESSION['feilds'] = array();
}
问题是您总是将$\u会话['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']);
?>
它将覆盖会话中以前存储的值。因为您正在使用相同的键覆盖值,因为您$\u SESSION['feilds']=array()
删除以前的数组并创建一个新的空数组,在其中推送新数据。
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 :)