【问题标题】:PHP variable and HTML text input valuePHP 变量和 HTML 文本输入值
【发布时间】: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 标记之后,例如 &lt;?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 处理。对于您正在尝试做的事情,可能有更好的解决方案。
  • 更改文件扩展名没有帮助。如果它是会话变量,变量不会持续存在吗?

标签: php html variables input


【解决方案1】:
 $wire_type = $_POST['wire_type_option']; // assignment operator 
 header("Location: ./guest.html"); // redirecting to guest.html

如果你想将 $wire_type 显示到另一个页面,你需要将它添加到会话变量中或需要创建 $_GET 请求,尽管我不建议使用 $_SESSION 变量。

您需要完成的修复 $_GET

 header("Location: ./guest.php?wi_type=".$wire_type); or  header("Location: ./guest.php?wi_type=".$_POST['wire_type_option']);

如何访问guests.php上的wi_type

echo $_GET['wi_type'];

修复$_SESSION

$_SESSION['wire_type'] = $_POST['wire_type_option']
header("Location: ./guest.php")

在 guest.php 上

echo $_SESSION['wire_type'];

如果您知道在不同页面上需要多次使用wire_type,请使用$_SESSION

-- 更新

session_start(); put it on the top 
if(isset($_POST['submit'])) {
       $_SESSION['wire_type']  = $_POST['wire_type_option'];
        header("Location: ./guest.php");
}
else {
        echo "loop";
}

您的客人文件必须具有顶部带有session_start(); 的.php ext。

【讨论】:

  • 我在 guest.php 中尝试过 但文本字段还是空白
猜你喜欢
  • 2013-05-12
  • 1970-01-01
  • 2015-04-08
  • 2015-05-11
  • 1970-01-01
  • 2012-11-10
  • 1970-01-01
  • 2015-04-24
  • 1970-01-01
相关资源
最近更新 更多