我想将文本文件填充到下拉菜单中。然后,将所选项目发布到另一个文本文件。
该脚本不起作用。它只将空白行写入“userinput.ini”。
我的index.php代码:
<html>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<body>
</body>
</html>
<form action="foo.php" method="post">
<input type="submit" value="Confirm">
</form>
<?php
$clientcode = "clientcode.csv";
$read_clientcode = @fopen($clientcode, "r") or die ("Couldn't Access $clientcode");
$clientcode_contents = fread($read_clientcode, filesize($clientcode));
$clientcode_array = explode("\n",$clientcode_contents);
fclose($read_clientcode);
echo '<select name="SITE">';
foreach($clientcode_array as $key => $value) {
echo '<option value="'.$key.'">'.$value.'</option>';
}
echo '</select>';
?>
我的foo.php代码:
<?
$fp = "userinput.ini";
$fh = fopen($fp,"w") or die ("Error opening file in write mode!");
fputs($fh,$_POST['SITE']. "\n");
fclose($fh);
?>
感谢您的建议!
答案 0 :(得分:1)
你必须要结束文件。 您的代码应如下
<html>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<body>
</body>
</html>
<form action="foo.php" method="post">
<input type="submit" value="Confirm">
<?php
$clientcode = "clientcode.csv";
$read_clientcode = @fopen($clientcode, "r") or die ("Couldn't Access $clientcode");
$clientcode_contents = fread($read_clientcode, filesize($clientcode));
$clientcode_array = explode("\n",$clientcode_contents);
fclose($read_clientcode);
echo '<select name="SITE">';
foreach($clientcode_array as $key => $value) {
echo '<option value="'.$key.'">'.$value.'</option>';
}
echo '</select>';
?>
</form>
答案 1 :(得分:0)
看一下PHP函数fgetcsv()
请读取csv文件以生成您的选择列表,然后在foo.php上再次获取该csv文件,这次使用它作为白名单来检查该值是否未被篡改为Pål布拉特贝格已正确评论。
两次拾取csv文件可能会让你考虑创建一个共享函数,或者如果这个csv文件只改变说,每周一次,你可能会咬紧牙关并重新编写csv作为一个简单的PHP数组一个文件 - 在上传新的csv时更新。
答案 2 :(得分:0)
问题已解决:
的index.php
<html>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<body>
</body>
</html>
<form action="foo.php" method="post">
<?php
$clientcode = "clientcode.csv";
$read_clientcode = @fopen($clientcode, "r") or die ("Couldn't Access $clientcode");
$clientcode_contents = fread($read_clientcode, filesize($clientcode));
$clientcode_array = explode("\n",$clientcode_contents);
fclose($read_clientcode);
echo '<select name="SITE">';
foreach($clientcode_array as $SITE) {
echo ("<option");
if ($HTTP_POST_VARS["SITE"] == $SITE) echo (" selected");
echo (">$SITE");
}
?></select><br>
<input type="submit" value="Confirm">
</form>
foo.php
<?php
echo $HTTP_POST_VARS['SITE'];
?>