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?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
puk789
  • 322
  • 2
  • 8
  • 28

4 Answers4

1

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

if (!empty($_POST["conv"])) {
$type = $_POST["conv"];
}
gknicker
  • 5,509
  • 2
  • 25
  • 41
chris85
  • 23,846
  • 7
  • 34
  • 51
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>
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
  • This actually doesnt print out anything so I guess it is not set, though I dont know what it means. – puk789 Feb 28 '15 at 23:16
  • @puk789 You must verify that you was send the page and $_POST exist. – Adrian Cid Almaguer Feb 28 '15 at 23:17
  • @puk789 If you want use $_POST["conv"] you must first submit the page – Adrian Cid Almaguer Feb 28 '15 at 23:18
  • 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 – puk789 Feb 28 '15 at 23:29
  • yeah I am just wondering why it doesnt work in the original file...very interesting – puk789 Feb 28 '15 at 23:40
  • read carefully and if you don't find anything just replace with my solution – Adrian Cid Almaguer Feb 28 '15 at 23:42
  • I mean, your solution doesnt work within my original file. I created another independent file and tested your solution and it works there though not in my file :D Confusing I know – puk789 Feb 28 '15 at 23:44
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>
Seshadri
  • 31
  • 3
-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.

Peter Bowers
  • 3,063
  • 1
  • 10
  • 18
  • 1
    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. – chris85 Feb 28 '15 at 23:15
  • 1
    I'm trying to think of an error that would come from $_POST['mysubmit'] that I wouldn't want to suppress in this case...? – Peter Bowers Feb 28 '15 at 23:16
  • 1
    Why suppress an error when you can resolve it/handle it correctly? – chris85 Feb 28 '15 at 23:27
  • 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. – Peter Bowers Mar 01 '15 at 05:30
  • 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... – chris85 Mar 01 '15 at 06:42
  • http://programmers.stackexchange.com/questions/219788/is-error-suppressing-bad-practice has some people (8) voting that error suppression is good if you are handling the error condition yourself (as I am) and others (5) voting that error suppression is always bad. Sounds like preference? – Peter Bowers Mar 01 '15 at 10:57
  • OP's choice is kind of irrelevant unless he/she can speak authoritatively as to whether this is preference or always bad. I truly want to know if something I do regularly is considered "not best practice" but I want something verifiable and not just an expressed preference. – Peter Bowers Mar 01 '15 at 10:59
  • Final message on the topic. http://php.net/manual/en/language.operators.errorcontrol.php "There may be a good reason for using outright error suppression in favor of the method I have suggested, however in the many years I've spent programming web apps I've yet to come across a situation where it was a good solution. The examples given on this manual page are certainly not situations where the error control operator should be used." – chris85 Mar 01 '15 at 16:14
  • That well expresses one person's preference. Here's a differing perspective from 2 well-known lead developers on a very mature open source project: http://www.pmichaud.com/pipermail/pmwiki-devel/2015-March/002255.html – Peter Bowers Mar 02 '15 at 07:14