我正在处理一个PHP代码,我会在页面上回显表单值和文件。我有这个工作正常,但我想实现一个启动按钮按下时将再次显示页面没有回声。有任何建议或更好的方法吗?
<html>
<head>
</head>
<body>
<div class="page">
<h1>File Loader</h1>
<form method="post">
<label>Name:</label><input type="text" name="name"></br>
<label>Date:</label><input type="text" name="date"></br>
<input type="submit" name="submit" value="Submit">
<input type="button" name="startOver" value="Start Over" onclick="phpfile.php"></br>
</form>
</div>
<?php
if ($_POST['submit']) {
$name = $_POST['name'];
$date = $_POST['date'];
echo "Name: $name</br></br>";
echo "Date: $date</br>";
$file_handle = fopen("file.csv", "r");
while (!feof($file_handle) ) {
$fields_of_text = fgetcsv($file_handle, 2024);
$f = $fields_of_text;
$format = "%s %s %s %s %s %s %s %s %s %s %s";
echo "<br>" . sprintf($format, $f[0], $f[1], $f[2], $f[3], $f[4], $f[5], $f[6], $f[7], $f[8], $f[9], $f[10]);
echo "<br>";
echo "<br>";
echo "<br>";
}
fclose($file_handle);
} else {
header("Location: phpfile.php");
}
?>
</div>
</body>
</html>
答案 0 :(得分:0)
您可以轻松使用查询字符串/ post params。
您可以使用如下所示的$ _GET,或者您可以使用隐藏输入,并使用onclick和javascript将其更改为1或2,如果您需要使用post。
</head>
<body>
<div class="page">
<h1>File Loader</h1>
<form method="post">
<label>Name:</label><input type="text" name="name"></br>
<label>Date:</label><input type="text" name="date"></br>
<input type="button" value="Submit" onclick="window.location.href='phpfile.php?SubmitType=1';">
<input type="button" value="Start Over" onclick="window.location.href='phpfile.php?SubmitType=2';">
</br>
</form>
</div>
<?php
if ($_GET['SubmitType'] == '1') {
$name = $_POST['name'];
$date = $_POST['date'];
echo "Name: $name</br></br>";
echo "Date: $date</br>";
$file_handle = fopen("file.csv", "r");
while (!feof($file_handle) ) {
$fields_of_text = fgetcsv($file_handle, 2024);
$f = $fields_of_text;
$format = "%s %s %s %s %s %s %s %s %s %s %s";
echo "<br>" . sprintf($format, $f[0], $f[1], $f[2], $f[3], $f[4], $f[5], $f[6], $f[7], $f[8], $f[9], $f[10]);
echo "<br>";
echo "<br>";
echo "<br>";
}
fclose($file_handle);
}
else {
header("Location: phpfile.php");
exit();
}
?>
</div>
</body>
</html>