PHP自引用脚本

I am trying to embed a self-referencing PHP script inside an HTML form with following code:

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>

And I am getting this messages:

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

Everything worked fine when the PHP script was in another file. Anyone knows what am I doing wrong?

You must verify that you was send the page and $_POST exist. And correct the select element

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

The variable ($type = $_POST["conv"];) is not set until the form is processed. Do

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

Your PHP code will run every time you load the page, not only when someone presses submit. This means it looks out there for $_POST['conv'] and $_POST['temperature2'] but doesn't find anything because the form hasn't been posted.

You need to name your submit button and then surround all your PHP processing with an if like this:

<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."; 
    }
}
?>

Now it will only look at that PHP code when someone has actually submitted something. Putting the @ before the @$_POST['mysubmit'] makes it so you don't get the same error that you were getting before on this new array key.