We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,248 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

php 'Undefined index' problem. simple task

I am new to php, just made a simple calculator. when I don't select the radio buttons; a notice come up.

Notice: Undefined index: r in E:\MY LOCAL SERVER\htdocs\php practise\calculator-form-advanced.php on line 59

Also I want show results in this input section

  <p>
  <label for="result">Result</label>
  <input type="text" value="" />
  </p>

---

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
 <p>
    <label for="num1">Number 1</label>
    <input type="text" name="num1" id="num1" />
  </p>
  <p>
    <label for="num2">Number 2</label>
    <input type="text" name="num2" id="num2" />
  </p>
  <p>
    <input type="radio" name="r" id="add" value="add" />
    <label for="add">Addition</label>
  </p>
  <p>
    <input type="radio" name="r" id="sub" value="sub" />
    <label for="sub">Subtraction</label>
  </p>
  <p>
    <input type="radio" name="r" id="mul" value="mul" />
    <label for="mul">Multiplication</label>
  </p>
  <p>
    <input type="radio" name="r" id="div" value="div" />
    <label for="div">Division</label>
  </p>

  <p>
    <input type="submit" name="submit" id="submit" value="Submit" />
  </p>

  <p>
    <label for="result">Result</label>
    <input type="text" value="" />
  </p>


</form>




<?php

        if (isset($_POST["submit"]))
            {
                $num1 = $_POST['num1'];
                $num2 = $_POST['num2'];
                $action = $_POST['r'];
                if ( $num1==''|| $num2=='' || $action=='' )
                    echo 'Something missing';

                else {


                switch ($action)
                {
                    case "add":
                    {
                        $result = $num1 + $num2;
                        echo $result;
                        break;

                    }
                    case "sub":
                    {
                        $result = $num1 - $num2;
                        echo $result;
                        break;

                    }case "mul":
                    {
                        $result = $num1 * $num2;
                        echo $result;
                        break;

                    }case "div":
                    {
                        $result = $num1 / $num2;
                        echo $result;
                        break;

                    }

                }
                }
            }
 ?>
</body>
</html>
2
Contributors
1
Reply
34 Minutes
Discussion Span
3 Months Ago
Last Updated
13
Views
rubai
Light Poster
28 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

try checking if the radio button has been selected before assigning it to a variable

dinhunzvi
Junior Poster in Training
74 posts since Jul 2010
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0578 seconds using 2.68MB