2012-07-31 121 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