2013-06-28 38 views
0

我想通過AJAX jQuery中 我阿賈克斯PHP頁面上發送一些值存儲在PHP數組或PHP會話值,並希望將它們存儲存儲值PHP數組/會議通過AJAX jQuery中

問題:每次陣列/會話返回最新SENT值,不是我送
我想,以前發送的值應該在陣列或會話保留以前的值

我的代碼如下

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']); 
?> 
+0

它將覆蓋在會議之前存儲的值。因爲你用$ _SESSION ['feilds'] = array()'刪除前一個數組並創建一個新的空數組,並在其中推送新數據,因此使用相同的鍵 –

+0

覆蓋值。 – Gntem

回答

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']); 
?> 
+0

非常感謝,現在問題解決了......上帝保佑你 –

+0

很高興我能幫上忙。沒關係 :) – machineaddict

0

這是由於$_SESSION['feilds'] = array();發生。

當調用頁面時$_SESSION['feilds']被賦值爲空數組。這就是爲什麼你只能得到當前值。

檢查$_SESSION['feilds']是已經存在或不使用issetempty

if(empty($_SESSION['feilds'])) { 
    $_SESSION['feilds'] = array(); 
} 
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 :)