PHP 自引用脚本

PHP Self-referencing script

我正在尝试使用以下代码在 HTML 表单中嵌入自引用 PHP 脚本:

Undefined index: conv

<form action = "<?php $_SERVER['PHP_SELF'] ?>" method = "post">
    <input type = "number" id = "temp2" name = "temperature2" placeholder = "28">
    <label for = "temp2"> degrees </label>

    <select>
        <option name = "conv" value = "f"> Fahrenheit </option> 
        <option name = "conv" value = "c"> Celsius </option>
    </select>   

    <input type = "submit" value = "equals">

    <?php
        $type = $_POST["conv"];
        $tmp = $_POST["temperature2"];
        if ($type == "f") {
            $newTmp = (9/5 * $tmp) + 32;
            echo $newTmp . " degrees Celsius.";
        }
        elseif ($type == "c") {
            $newTmp = (5 * ($tmp - 32)) / 9;
            echo $newTmp . " degrees Fahrenheit."; 
        }
    ?>

</form>

我收到了这条消息:

注意:未定义索引:conv
注意:未定义索引:temperature2

当 PHP 脚本在另一个文件中时一切正常。 有人知道我做错了什么吗?

在处理表单之前不会设置变量 ($type = $_POST["conv"];)。做

if (!empty($_POST["conv"])) {
$type = $_POST["conv"];
}

您必须确认您发送了页面并且 $_POST 存在。并更正 select 元素

<form action = "<?php $_SERVER['PHP_SELF'] ?>" method = "post">
    <input type = "number" id = "temp2" name = "temperature2" placeholder = "28">
    <label for = "temp2"> degrees </label>

<select name = "conv">
    <option  value = "f"> Fahrenheit </option> 
    <option  value = "c"> Celsius </option>
</select>   

    <input type = "submit" value = "equals">

    <?php

        if(isset($_POST["temperature2"])) {        

        $type = $_POST["conv"];
        $tmp = $_POST["temperature2"];
        if ($type == "f") {
            $newTmp = (9/5 * $tmp) + 32;
            echo $newTmp . " degrees Celsius.";
        }
        elseif ($type == "c") {
            $newTmp = (5 * ($tmp - 32)) / 9;
            echo $newTmp . " degrees Fahrenheit."; 
        }
}
    ?>

</form>

您的 PHP 代码将 运行 每次 加载页面,而不仅仅是当有人按下提交时。这意味着它在那里寻找 $_POST['conv']$_POST['temperature2'] 但没有找到任何东西,因为表单尚未发布。

您需要为您的提交按钮命名,然后用 if 围绕您所有的 PHP 处理,如下所示:

<input type = "submit" name="mysubmit" value = "equals">

<?php
if (@$_POST['mysubmit']) {
    $type = $_POST["conv"];
    $tmp = $_POST["temperature2"];
    if ($type == "f") {
        $newTmp = (9/5 * $tmp) + 32;
        echo $newTmp . " degrees Celsius.";
    }
    elseif ($type == "c") {
        $newTmp = (5 * ($tmp - 32)) / 9;
        echo $newTmp . " degrees Fahrenheit."; 
    }
}
?>

现在它只会在有人实际提交内容时查看 PHP 代码。将 @ 放在 @$_POST['mysubmit'] 之前,这样您就不会在这个新数组键上得到与之前相同的错误。

这是我的答案... 首先,最好验证它是否已提交 ??,如果提交按钮被调用,则代码继续休息。否则你会出错。此外,在您单击提交按钮之前,不会显示结果和变量。

<form action = "<?php $_SERVER['PHP_SELF'] ?>" method = "post">
    <input type = "number" id = "temp2" name = "temperature2" placeholder = "28">
    <label for = "temp2"> degrees </label>

<select name = "conv">
    <option  value = "f"> Fahrenheit </option> 
    <option  value = "c"> Celsius </option>
</select>   

    <input type = "submit" name="submit" value = "equals">

    <?php

        if(isset($_POST["submit"])) {        

        $type = $_POST["conv"];
        $tmp = $_POST["temperature2"];
        if ($type == "f") {
            $newTmp = (9/5 * $tmp) + 32;
            echo $newTmp . " degrees Celsius.";
        }
        elseif ($type == "c") {
            $newTmp = (5 * ($tmp - 32)) / 9;
            echo $newTmp . " degrees Fahrenheit."; 
        }
}
    ?>

</form>