0

I need help. It seems that the code has one error: it says that I have undefined index:number, but I already declared a name for my textbox with the name number. I also wrote code that if $_POST['number'] == '', but it seems nothing has fixed this. Please help me.

<html>
        <head>
            <title>SUBMIT</title>
        </head>
        <body>
        <form action="" method='post'>
            <input type='text' name='number'><br><br>
            <input type='submit' name='celsius' value='celsius'>
            <input type='submit' name='fahrenheit' value='fahrenheit'>
            </form>
            <?php
                function celsius($x){
                        $cel=($x -32) * 5/9;
                        return $cel;


                }
                function fahrenheit($x){
                        $far=$x * 9/5 + 32;
                        return $far;


                }

                if($_POST['number']== ""){
                        echo 'plese input a number';

                }
                else if($_POST['number'] != is_numeric($_POST['number'])){

                    echo 'type only numbers';

                }
                else if(isset($_POST['celsius'])){
                        $x=$_POST['number'];
                        if($_POST['celsius']){

                            echo $x.' Fahrenheit is '.celsius($x);

                        }





                }else if(isset($_POST['fahrenheit'])){
                    $x=$_POST['number'];
                        if($_POST['fahrenheit']){

                            echo $x.' Fahrenheit is '.fahrenheit($x);

                        }

                }


            ?>
        </body>
    </html>
kittykittybangbang
  • 2,380
  • 4
  • 16
  • 27
  • You don't check if the form is submitted before trying to access `$_POST` values. `if($_POST['number']== ""){` If the form isn't submitted there is no `$_POST['number']`. You could change that to `!empty` and then you wont get notice, if that is the notice point (no line number). `A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.` http://php.net/manual/en/function.empty.php – chris85 Aug 15 '15 at 00:49
  • Weird, just tried your whole script and it is working for me. [Check here](http://nightingale-family.com/projects/calculatetemp/). I did not modify anything. Am I missing what the problem is? – Joshua Nightingale Aug 15 '15 at 01:00
  • @JoshuaNightingale do you have error reporting on, and if so on what level? This isn't an error, it is only a notice. – chris85 Aug 15 '15 at 01:00
  • @chris85 my bad forgot to add the lines error_reporting(-1); ini_set('display_errors', 'On'); to his code :P – Joshua Nightingale Aug 15 '15 at 01:05

4 Answers4

0

I think that you should be checking to see if $_POST['number'] isset, so:

if(isset($_POST['number'])==""){
// do some stuff
} else {
// do some more stuff
} etc ...
Filthy_Rich
  • 666
  • 5
  • 20
0

try enclosing an if statement over your whole php code that checks if the post is set

<?php
   if (isset($_POST['number'])) {

      // all the php code

   }
?>
gommb
  • 1,121
  • 1
  • 7
  • 21
  • Hello thanks you can u explain Y i need to isset the input text is it because to check if the value is null or not im now confused thanks – Shauiosbhaoiu Aousiguosba Aug 15 '15 at 01:53
  • the `isset()` function determines if a variable exists or doesn't exist aka null. If you perform an if statement on a variable that doesn't exist it will return an error. This way it will check that the variable exists before running if statements on it therefore preventing any errors – gommb Aug 15 '15 at 01:59
0

This is the Updated working version :D

<html>
    <head>
        <title>SUBMIT</title>
    </head>
    <body>
    <form action="" method='post'>
        <input type='text' name='number'><br><br>
        <input type='submit' name='celsius' value='celsius'>
        <input type='submit' name='fahrenheit' value='fahrenheit'>
        </form>
        <?php
            function celsius($x){
                    $cel=($x -32) * 5/9;
                    return $cel;


            }
            function fahrenheit($x){
                    $far=$x * 9/5 + 32;
                    return $far;


            }

            if(isset($_POST['number'])){


             if($_POST['number']==''){
                 echo 'please input a number';

             }
            else if($_POST['number'] != is_numeric($_POST['number'])){

                echo 'type only numbers';

            }
            else if(isset($_POST['celsius'])){
                    $x=$_POST['number'];
                    if($_POST['celsius']){

                        echo $x.' Fahrenheit is '.celsius($x);

                    }





            }else if(isset($_POST['fahrenheit'])){
                $x=$_POST['number'];
                    if($_POST['fahrenheit']){

                        echo $x.' Fahrenheit is '.fahrenheit($x);

                    }

            }
            }


        ?>
    </body>
</html>
0

I am still learning PHP myself so used your code and tried to fix it the best I could. I also changed a few things around. It looked as if the math for the functions was set wrong but could have been me (I changed it a bit) Also I am not much of a fan using "else if" statements so I changed it around a bit using only "if" and "else", was going to try using the catch and switch but changed my mind.

If someone would let me know their own opinions about the changes I made to his file for my own learnings I would appreciate it. (What I could have done different or even if I am all wrong by the way I did it.)

Working example here --> projects/calculatetemp/

<form action="" method='post'>
<input type='text' name='number'><br><br>
<input type='submit' name='Celsius' value='Celsius'>
<input type='submit' name='Fahrenheit' value='Fahrenheit'>
</form>
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
function celsius($x){
    $cel=$x*9/5+32;
    return $cel;
}

function fahrenheit($x){
    $far=5/9*($x-32);
    return $far;
}

if(isset($_POST['number'])){
    if(!empty($_POST['number'])){
        if($_POST['number'] != is_numeric($_POST['number'])){
            echo 'type only numbers';
        }else{
            if(isset($_POST['Celsius'])){
                $x=$_POST['number'];
                if($_POST['Celsius']){
                    echo $x.' Celsius = '.celsius($x).' Fahrenheit';
                }
            }
            if(isset($_POST['Fahrenheit'])){
                $x=$_POST['number'];
                if($_POST['Fahrenheit']){
                    echo $x.' Fahrenheit = '.fahrenheit($x).' Celsius';
                }
            }
        }
    }else{
        echo 'You have to input a value';
    }
}else{
    echo 'Please input a temperature to have it converted';
}
?>