0

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?

3
  • possible duplicate of PHP: "Notice: Undefined variable" and "Notice: Undefined index" Commented Feb 28, 2015 at 23:11
  • Does having the name attribute on the option tag actually work? Commented Feb 28, 2015 at 23:12
  • @puk789 don't forget check my answer as correct ;-) Commented Feb 28, 2015 at 23:39

4 Answers 4

1

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>
Sign up to request clarification or add additional context in comments.

7 Comments

This actually doesnt print out anything so I guess it is not set, though I dont know what it means.
@puk789 You must verify that you was send the page and $_POST exist.
@puk789 If you want use $_POST["conv"] you must first submit the page
Well, it doesnt work in my original file but I created a completely new file and it works fine there. Dont know what the problem is, as I checked all my variables in my original file and there were no duplicates. Thanks though
yeah I am just wondering why it doesnt work in the original file...very interesting
|
1

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

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

4 Comments

or possibly better just check if (!empty($_POST)) { around that whole block that is suppose to process when the form is submitted.
I think the other error is that the select should have the name attribute.
I included name attribute in "select".
The code above has it in the option elements. Is your code working as expected now?
0

Here is my answer... First, it is better to verify, is it submitted or not ??, if submit button is invoked then code proceed rest. Else you get error. Moreover result and variable will not be shown until you click the submit button.

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

Comments

-1

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.

9 Comments

Don't use the @. That suppresses the errors and could hide errors in the future. There are functions built for this already, e.g. empty and isset.
I'm trying to think of an error that would come from $_POST['mysubmit'] that I wouldn't want to suppress in this case...?
Why suppress an error when you can resolve it/handle it correctly?
Handling it correctly, in this case, is suppressing it. Did you have any theoretical examples of when this suppression could suppress an error that I don't want to suppress? Downvoting by reflex whenever you see an @ is pretty poor form, in my book. Thnk about it and consider whether it is an appropriate use of the feature of the language first. Can @ be misused and abused? Absolutely! But does it have a place and can it be used appropriately? Absolutely.
How is suppressing an error/notice correct? Quicker than finding the issue? Sure. Correct? No. Look at my history I've down voted one time, here. We could also leave it to the accepted answer here to resolve this debate...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.