【发布时间】:2015-06-04 07:04:44
【问题描述】:
我试图在 HTML 文本输入字段的值中显示 PHP 变量的值。目前提交后的文本输入字段只打印 PHP 脚本的内容。该脚本名为 submit_type.php
<?php
if(isset($_POST['submit'])) {
$wire_type = $_POST['wire_type_option'];
header("Location: ./guest.html");
}
else {
echo "loop";
}
?>
我已经尝试了问题Using a PHP variable in a text input value = statement的解决方案 如 guest.html 第 39 行所示,但它似乎不起作用
<input type="text" name="type" value="<?php echo htmlspecialchars($wire_type); ?>" />
index.html 主页面上的提交按钮将您重定向到 guest.html(几乎与 index.html 文件相同,除了第 39 行设置了值)
供参考,这里是guest.html
<!DOCTYPE html>
<html>Current
<head>
<title> Thermocouple Database</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<section>
<h1> Login to upload a calibration</h1>
<form>
<div class='login'><label for="username"> Username: </label></div>
<input type="text" name="username">
<div class='pass'><label for="password"> Password: </label></div>
<input type="text" name="password">
<input type="submit" value="Submit">
<br> <br> <br> <br>
</form>
</section>
<section>
<h1> Which thermocouple type do I have? </h1>
<label> My wire looks like: </label>
<form method="post" action="submit_type.php">
<select name='wire_type_option'>
<option value="J-type"> J-type </option>
<option value="K-type"> K-type </option>
<option value="T-type"> T-type </option>
<option value="E-type"> E-type </option>
</select>
<input type="submit" name="submit" value="Submit wire type">
</form>
<!-- <form method="get" action="action.php"> -->
<div id="results">
Result: <label name="result_label"> </label>
<div id="result_table">
<label> Type: </label>
<input type="text" name="type" value="<?php echo htmlspecialchars($wire_type); ?>" />
<label> Temperature Range: </label>
<input type="text" name="min_temp">
<label> - </label>
<input type="text" name="max_temp">
<br> <br> <br>
<label> Published calibration </label>
<br> <br>
<label> Voltage: </label>
<input type="text" name="in_voltage">
<label> Calibrated Temperature: </label>
<input type="text" name="out_temp">
<br> <br>
<label> Temperature: </label>
<input type="text" name="in_temp">
<label> Calibrated Voltage: </label>
<input type="text" name="out_voltage">
</div>
</div>
<!-- <input type="submit" value="Submit"> -->
<!-- </form> -->
</section>
</body>
</html>
我在这里缺少什么?我还是 Web 开发的新手,因此感谢所有帮助!
【问题讨论】:
-
将错误报告添加到文件顶部,紧跟在打开 PHP 标记之后,例如
<?php error_reporting(E_ALL); ini_set('display_errors', 1);,然后是其余代码,看看它是否产生任何结果。 -
你的页面有html的扩展名,把它改成php,以便在将页面发送到浏览器之前执行php代码。
-
"主 index.html 上的提交按钮" - 您是否指示 Apache 将
.html文件视为 PHP?如果没有,那么您将无法运行 PHP 指令。将其重命名为.php或告诉 Apache 将它们视为 PHP。 guest.html 和您可能尝试运行 PHP 指令的任何其他.html文件也是如此。 -
不确定该变量是否会持续存在,即使您将其更改为作为 PHP 处理。对于您正在尝试做的事情,可能有更好的解决方案。
-
更改文件扩展名没有帮助。如果它是会话变量,变量不会持续存在吗?