用于保存数据的PHP问题

时间:2013-09-09 00:21:16

标签: php html

我的任务是将表单数据保存到文本文件。但是,php没有被执行.. 即使在点击提交按钮后,文本文件也是空的.. 请让我知道这些缺陷 MYHTML

 <form id="form" name="form" method="post" action="Input2.php">


 <label>Choose my Map set  :
 </label>
 <select name="Mapset">
  <option value="Global network">Global Network</option>

  </select> <br> 
 <br>

<label>Tiff code:
</label>
<select name="Tiff">
<option value="MX">MX</option>
  </select> <br> <br>
<label>Physical size :
</label>
<input type="text" name="size" size="10"><br>
<label>time:
</label>
<input type="text" name="time" size="10"><br>
<div style="text-align: center"><br>

<input type="submit" name="submit" id="submit" value="Next" class="submit">

<div class="spacer"></div> 
</form>

我的PHP:

if (isset($_POST['submit'])) { 
$data = $_POST['size'];
$data = $_POST['time'];

$file = "input.txt"; 

$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
 fwrite($fp, $data) or die("Couldn't write values to file!"); 

fclose($fp); 
echo "Saved to $file successfully!";
}
header("location:NetOptInput3.html");

1 个答案:

答案 0 :(得分:1)

这里尝试(测试)

您需要指定$_POST值才能告诉它“保存在文件中”。

这样做的方法是它将占用所有POST值。

您还可以像这样分配单个变量:

$size=$_POST['size'];

添加了备注:

你有header("location:NetOptInput3.html");并且由于缺少空间而会失败。

这是正确的header("Location: NetOptInput3.html");

方式

HTML表单(我删除了name="submit",因为它也会显示在文件中)

<form id="form" name="form" method="post" action="Input2.php">
 <label>Choose my Map set  :
 </label>
 <select name="Mapset">
 <option value="Global network">Global Network</option>
 </select> <br> 
 <br>

<label>Tiff code:
</label>
<select name="Tiff">
<option value="MX">MX</option>
 </select> <br> <br>
<label>Physical size :
</label>
<input type="text" name="size" size="10"><br>
<label>time:
</label>
<input type="text" name="time" size="10"><br>
<div style="text-align: center"><br>

<input type="submit" id="submit" value="Next" class="submit">

<div class="spacer"></div> 
</form>

PHP处理程序Input2.php

注意:使用a开关会附加/添加到文件中,而w将覆盖之前保存的所有内容。

<?php

foreach($_POST as $data) {
$info = '';
$info .= $data . "\n";
$file = "data.txt";
$fp = fopen($file, "a") or die("Couldn't open $file for writing!");

fwrite($fp, $data . "\n") or die("Couldn't write values to file!");
}
fclose($fp); 

// You cannot use both header and echo. Choose one.
// header("Location: NetOptInput3.html");

echo "Success";

?>