0

我想通过 jquery 中的 ajax 将值存储在 PHP 数组或 PHP 会话中我通过 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']);
?>
4

3 回答 3

0
    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 :)
于 2013-06-28T07:29:41.977 回答
0

这是由于$_SESSION['feilds'] = array();.

调用页面时$_SESSION['feilds']分配空白数组。这就是为什么您只获得当前值的原因。

通过使用或检查$_SESSION['feilds']是否已经存在issetempty

if(empty( $_SESSION['feilds'] )) {
  $_SESSION['feilds'] = array();
}
于 2013-06-28T07:02:51.147 回答
0

问题是您$_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-28T07:03:54.880 回答