我想存储在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']);
?>https://stackoverflow.com/questions/17358968
复制相似问题