【发布时间】:2015-12-05 03:13:55
【问题描述】:
以下代码是我正在使用的代码:
<form action="sendinfo.php" method="post" id="form">
-Title:<br>
<input type="text" class="u-full-width" name="title" placeholder="Insert a title" >
<br>-Date<br>
<input class="u-full-width" type="text" name="date" placeholder="Click to add a date"><hr>
<br>-Buildyear:<br>
<input class="u-full-width" type="text" name="buildyear" placeholder="click to add buildyear">
<br>-rebuild<br>
<input id="1" type="radio" name="rebuildyes" value="yes"><label for="1">tes</label>
<input id="2" type="radio" name="rebuildno" value="No"><label for="2">No</label><hr>
<input type="submit" value="send" class="u-full-width">
</form>
另一个代码是:
<?php
//variables
$title = $_POST['title'];
$date = $_POST['date'];
$buildyear = $_POST['buildyear'];
$rebuildyes = $_POST['rebuildyes'];
$rebuildno = $_POST['rebuildno'];
//in browser
echo "<h1>The inserted data has been locally stored as " . $title . " " . $date . ".txt</h1>";
echo "<p>click <a href='index.html'>Here</a> to return to the previous screen</p>";
//write text file.
$file = fopen($title . " " . $date . ".txt","w");
if ($date) fwrite($file, "Date: " . $date . "\r\n");
fwrite($file, "\r\n");
if ($buildyear) fwrite($file, "Buildyear: " . $buildyear . "\r\n");
if ($rebuildyes) fwrite($file, "Rebuild? " . $rebuildyes . "\r\n");
if ($rebuildno) fwrite($file, "Rebuild? " . $rebuildno . "\r\n");
?>
此代码的结果是一个生成的文本文件,其中数据填写在第一页给出的表格中。这里的问题是我无法让单选按钮工作,至少我无法从中获取数据。当它们为空时,不应将它们写在文件中,当它们被选中时,它们应该是。
另外,请不要告诉我这是一种危险的 PHP 编码方式,因为我知道它让用户可以在 PHP 可以访问的任何地方进行编写,但是这个“项目”只能供少数人使用,并且实际上不会托管在服务器上。话虽这么说,如果你有更好的方法让我构建这个项目,而不是风险明智,只是 PHP 明智,请告诉我,因为我刚刚开始编写 PHP。
谢谢
【问题讨论】:
标签: php html forms get radio-button