PHP自引用脚本

时间:2015-02-28 23:08:40

标签: php forms self-reference

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

  

未定义索引:转化

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

我收到这条消息:

Notice: Undefined index: conv
Notice: Undefined index: temperature2

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

4 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

您必须验证您是否发送了该页面并且$ _POST存在。并更正选择元素

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

答案 2 :(得分:0)

这是我的答案... 首先,最好验证一下是否提交了,如果调用了提交按钮,则代码继续休息。否则您会出错。此外,只有单击提交按钮,结果和变量才会显示。

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

答案 3 :(得分:-1)

您的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']之前放置@使得你不会得到你之前在这个新数组键上得到的相同错误。

相关问题