PHP变量和HTML文本输入值

时间:2015-03-31 03:42:32

标签: php html variables input

我试图在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>

我在这里缺少什么?我还是网络开发的新手,所以我们非常感谢所有人的帮助!

1 个答案:

答案 0 :(得分:0)

 $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")

on 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。