【发布时间】:2012-08-01 10:14:45
【问题描述】:
大家好,我有这个问题,我无法比较这两个字符串
<?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);
}
$flag = 0;
$stringData = $_POST['data']; // echo = null
$file = "/var/www/devData/test/ciao.txt";
$fh = fopen($file, 'a+') or die("can't open file");
while(!feof($fh)) {
$theData = fgets($fh, filesize($file));
array_push($arr,$theData);
$i++;
}
for($j=0; $j<count($arr); $j++)
if($stringData == $arr[j]){ // is the problem
$flag = 1;
}
if($flag = 0){
fwrite($fh, $stringData); // fwrite works perfectly even if i try to print $string the result is null
}
fclose($fh);
?>
谁能解释我如何解决这个问题?这个脚本的目标是避免用户写两次相同的东西
【问题讨论】:
-
所以我们应该猜猜为什么
$_POST['data']是未定义的?我的猜测:您的表单有问题。 -
不是因为我可以将 $_POST 的内容写入 stange 的文件中
-
我看到这是用
ajax标记的,您是否将数据作为帖子传递?你在做什么样的ajax请求?你在使用像 jQuery 这样的库吗.. 这么多问题!! -
$stringData = $_POST['data']; // echo = null暗示数据从一开始就不存在。所以问题是:它应该从哪里来? -
您的表单中是否有任何包含名称数据的输入字段?