2012-07-31 64 views
0

我想顯示POST變量的內容,但是我的輸出是空的。顯示內容POST

<?php 
$dir = '/var/www/devData/test'; 

// create new directory with 777 permissions if it does not exist yet 
// owner will be the user/group the PHP script is run under 
if (!file_exists($dir)) { 
    mkdir ($dir, 0777); 
} 


     $stringData = print_r($_POST['data'], true); 
     $file = "/var/www/devData/test/ciao.txt"; 
     $fh = fopen($file, 'a+') or die("can't open file"); 
     $fla = 0; 

     $arr = array(); 
     $i = 0; 
     while(!feof($fh)) { 
        $theData = fgets($fh, filesize($file)); 
        array_push($arr,$theData); 
        //echo $arr[$i]; 
        $i++; 
     } 
     echo $stringData; 


     fclose($fh); 



?> 

當我調用該函數fwrite,可變$stringData被存儲到文件中。

這是調用PHP的文件中的函數:

$.post("JS/foo.php", {data: options_label}, function(result){alert(options_label) }, "json"); 

其中options_label是一個數組。

我試着用decode_json等等,但$stringData仍然是空的。

+0

你想顯示什麼? – Don 2012-07-31 19:15:23

+0

變量的含義$ stringData – Edivad 2012-07-31 19:22:58

+0

但這就是爲什麼我問......你是什麼意思的「contenute」? – Don 2012-07-31 19:23:53

回答

0

我認爲這個問題就出在這裏:

$stringData = print_r($_POST['data'], true); 

如果你想的$ _ POST的內容存儲在file.txt的則應將$ _ POST [「數據」]爲一個字符串,像這樣:

$stringData = ""; 

foreach ($_POST['data'] as $key=>$val) { 
    $stringData .= $key . ": " . $val . "\n\r"; 
} 
+0

Matt我可以將$ stringData的內容存儲到文件中,我只想顯示它並在 – Edivad 2012-07-31 20:03:20

+0

之後操作它。上面的代碼將允許您這樣做。 – Matt 2012-07-31 20:04:23

+0

輸出仍爲NULL <?php \t \t $ stringData =「」; \t \t的foreach($ _POST [ '數據']爲$鍵=> $ val)的{ \t \t \t \t $ StringData是。= $鍵。 「:」。 $ val。 「\ n \ r」; \t \t} \t \t $ stringData =「」; $ stringData = $ _POST ['data']; $ file =「/var/www/devData/test/ciao.txt」; $ fh = fopen($ file,'a +')或死(「無法打開文件」); \t \t fwrite($ fh,$ stringData); \t \t var_dump($ stringData); fclose($ fh); ?> – Edivad 2012-07-31 20:08:45