0

我只是想将一些表单数据输出到文本文件。我无法让它工作。有人能帮忙吗?我得到一个错误invalid offset type。感谢所有的帮助!

这是php

$file = "output.txt";

$current = file_get_contents($file);

foreach ($_POST as $key) {
    $current .= $_POST["data"][$key];
}

file_put_contents($file, $current);

这是html

<form name="input" action="getinput.php" method="post">
  Name: <br /><input type="text" name="data[]" placeholder="First and Last"><br />
  ID: <br /><input type="text" name="data[]" placeholder="Your ID"><br />
  Major: <br /><input type="text" name="data[]" placeholder="Your Major"><br />
  Email: <br /><input type="email" name="data[]" placeholder="Email Address"><br />
  Phone: <br /><input type="text" name="data[]" placeholder="XXX-XXX-XXXX"><br />
  Addition Information: <br /><textarea name="data[]" cols="30" rows="10" placeholder="Please enter any extra  or comments you would like me to have or know."></textarea><br />
  <input type="submit" value="Submit">
</form>
4

2 回答 2

0

怎么样:

foreach ($_POST['data'] as $key => $value) {
   $current .= $value;
}

现在,您$_POST不仅要遍历包含您的部分的整个数组,这就是您在执行以下操作时收到警告$_POST['data']的原因:Invalid offset

$_POST["data"][$key];
于 2013-10-30T01:21:08.980 回答
0

foreach的不对。你想要这样的东西来输出而不是

foreach($_POST['data'] as $value) {

    $current .= $value;
}
于 2013-10-30T01:21:34.363 回答