0

When I say 'new', I mean that this is just about the first time I'm attempting php.

Anyway. I keep getting this error notice "Undefined index: type in c:\x\calculator.php on line 33", but it still echoes "You forgot to pick mathtype!" and the calculator works fine. This error notice occurs only when I don't select any radio box for math type (+-/*).

//Part of the form
<form action="calculator.php" method="post">
<input type="text" name="1stnumber">
<input type="text" name="2ndnumber">
<input type="radio" name="type" value="addition">
<input type="radio" name="type" value="subtraction">
<input type="submit" name="send">

<?php
//My variables
$number = $_POST['1stnumber']
$numbero = $_POST['2ndnumber']
$mathtype = $_POST['type'] /* **<-line 33** */

//The calculation part of the form here, which is working

//Tell the user if he didn't pick a math type (+-)
if(is_null($mathtype)){
  echo "You forgot to pick mathtype!"
  }
?>

Tried with elseif as well.. I don't see what's wrong between line 33 and the if(is_null()) line!

Sorry if it looks poor, messy, or if something doesn't make sense. Might be a few typos as well. Any help is appreciated.

Casey Rule
  • 2,085
  • 2
  • 18
  • 28
  • 1
    try to check isset($mathtype) – ajtamwojtek Dec 11 '14 at 19:22
  • Did you remember to wrap the entire page in ` .. ` tags? – warren Dec 11 '14 at 19:23
  • When you're asking other people to put in the time to help you, instead of apologizing for messy formatting or typos, take the time to actually correct these things yourself. – Casey Rule Dec 11 '14 at 19:24
  • @warren `html` `body` `head` tags can be omitted in html – Jay Harris Dec 11 '14 at 19:25
  • 1
    Unchecked checkboxes and radio buttons are not posted to the server, so you should check if the "type" index is even set before trying to read it out. – Cᴏʀʏ Dec 11 '14 at 19:25
  • Please take the time to learn how to [interpret errors and fix your code](http://jason.pureconcepts.net/2013/05/fixing-php-errors/). – Jason McCreary Dec 11 '14 at 19:25
  • @CaseyRule It's quite a long form, and I don't have internet access on the computer I have the form on, so I did it all by hand. I do not know how to not make it look messy either. –  Dec 11 '14 at 19:28
  • @warren I'm new to php, but not that new to html. It's correctly wrapped. –  Dec 11 '14 at 19:28

4 Answers4

1

Simply check if type is posted before you pick it up

if(isset($_POST['type']))
{
   $mathtype = $_POST['type'];
}
else
{
    echo "Type was not selected";
}
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • Should this be right after my variables? –  Dec 11 '14 at 19:32
  • "Simply check if type is posted before you pick it up" Would that mean that it'd first see if a box is checked, and then run the rest/just return if one isn't? Finding it hard to make sense of it all! –  Dec 11 '14 at 19:33
1

Set a default selected option, using the checked attribute.

<label><input type="radio" name="type" value="addition" checked="checked"> +</label>
<label><input type="radio" name="type" value="subtraction"> -</label>

Don't forget inputs needs labels in html if left out you can use a placeholder attribute, but that's clearly not possible with type="radio"; therefore wrap the input in a label with the text description next to it eg + or -

Also, is this a copy and paste error, bc all php statements must be terminated with a semicolon ;

$number = $_POST['1stnumber'];       // <- terminate
$numbero = $_POST['2ndnumber'];      // <- terminate
$mathtype = $_POST['type'];          // <- terminate

echo "You forgot to pick mathtype!"; // <- terminate
Jay Harris
  • 4,201
  • 17
  • 21
0

Check if the form is posted:

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    //My variables
    $number = $_POST['1stnumber']
    $numbero = $_POST['2ndnumber']
    $mathtype = $_POST['type'] /* **<-line 33** */

    //The calculation part of the form here, which is working

    //Tell the user if he didn't pick a math type (+-)
    if(is_null($mathtype)){
      echo "You forgot to pick mathtype!"
      }
}
?>

Else the is_null check will also be executed on the first load (before the form has been posted).

Peter
  • 1,658
  • 17
  • 23
0

It's always good practice to check if the variable you're trying to retrieve from $_POST has actually been set, try this:

<?php
    //My variables
    if (isset($_POST['1stnumber'])) {
        $number = $_POST['1stnumber'];
    }
    if (isset($_POST['2ndnumber'])) {
        $numbero = $_POST['2ndnumber'];
    }
    if (isset($_POST['type'])) {
        $mathtype = $_POST['type']; /* **<-line 33** */
    }

    //The calculation part of the form here, which is working

    //Tell the user if he didn't pick a math type (+-)
    if (is_null($mathtype)) {
        echo "You forgot to pick mathtype!";
    }
?>
Edgar
  • 238
  • 1
  • 7