Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

jamesjones21
jamesjones21
9,260 Points

Help with undeclared functions in PHP script

Hi I get 3 undefined errors for my forms passing information to the variables inside my PHP script:

Basically I have tried the isset() around $_POST for $valA, $valB and $operator, but when this is done the values are not passed as they should. Any help would be appreciated.

<?php
/* -------------------  Declared Variables --------------------- */
//Gathers information passed through the textboxes

$valA = $_POST['Value_A'];
$valB = $_POST['Value_B'];
$operator = $_POST['operator'];

//this sets the variables to blank
$valA = $valB = "";
$valAerr = "";
//Answer for all
$answer = "";

/*-------------------- Declared Variables end ---------------------*/

/*-------------------- Form Validation start -----------------------*/

//Calculations set within the functions below

    //validation check if value A has an entry
    if ($_SERVER["REQUEST_METHOD"] == "POST"){
      if (empty($_POST["Value_A"])) {
        $valAerr = "Value Reqired";
        } 
    }

/*------------------ Form Validation end ----------------------------*/
/*---------------------------------- Function Start ----------------------------------*/
//Functions to be called within the switch statement based on the operator used

function add(){
    global $valA, $valB, $answer;

    $answer = $valA + $valB;
    return $answer;
}

function divide(){
  global $valA, $valB, $answer;

 $answer = $valA / $valB;
    return $answer;
}

function multi(){
  global $valA, $valB, $answer;

    $answer = $valA * $valB ;
    return $answer;
}


function subtract(){
  global $valA, $valB, $answer;

    $answer = $valA - $valB;
    return $answer;

}

/*----------------------- Function end ---------------------------------*/


/*----------------------- Switch Statement Start ------------------------*/
//switch statement to determine which function to call and to return the answer

switch ($operator){

  case "+":
      echo add();
      break;

  case "-":

   echo subtract();
   break;

  case "*":
    echo multi();
    break;

  case "/":
  echo divide();
  break;

  default:
  echo "Require an answer";
}

/*------------------------------------- Switch Statement End ----------------------------*/

?>
<!--- forms html --->

<form action="" method="post"><!-- need to define a php script to go here -->
        <label>Value A:<?php echo $valAerr; ?></label> <input type="text" name="Value_A"/>
    <label>Add an operator</label> <input type="text" name="operator"/>
        <label> Value B:</label> <input type="text" name="Value_B"/><br/><br/>
        <label>Answer:</label><?php echo $answer;?><!--<input type="text" name="Answer">--><br/><br/>
      <label>Value A: <?php echo $valA; ?></label><br/>
      <label>Value B: <?php echo $valB; ?></label><br/>
      <label>Operator: <?php echo $operator; ?></label><br/>
        <input type="submit" name="Get Answer" value="Get Answer">
    </form>

1 Answer

jamesjones21
jamesjones21
9,260 Points

altered code to check if values were set in the form ready to except data into the PHP script:

updated code:

<?php

//this sets the variables to blank
$valA = $valB = "";
$valAerr = "";
//Answer for all
$answer = "";
$operator="";

//Gathers information passed through the textboxes
if (isset($_POST['Value_A'])){
$valA = $_POST['Value_A'];

    if (isset($_POST['Value_B'])){
    $valB = $_POST['Value_B'];

            if (isset($_POST['operator'])){
            $operator = $_POST['operator'];
        }
    }
}



//Calculations set within the functions below

    //validation check if value A has an entry
    if ($_SERVER["REQUEST_METHOD"] == "POST"){
      if (empty($_POST["Value_A"])) {
        $valAerr = "Value Reqired";
        } 
    }


//Functions to be called within the switch statement based on the operator used

function add(){
    global $valA, $valB, $answer;

    $answer = $valA + $valB;
    return $answer;
}

function divide(){
  global $valA, $valB, $answer;

 $answer = $valA / $valB;
    return $answer;
}

function multi(){
  global $valA, $valB, $answer;

    $answer = $valA * $valB ;
    return $answer;
}


function subtract(){
  global $valA, $valB, $answer;

    $answer = $valA - $valB;
    return $answer;

}


//switch statement to determine which function to call and to return the answer

if(isset($_POST['operator'])){
  switch ($operator){

  case "+":
      echo add();
      break;

  case "-":

   echo subtract();
   break;

  case "*":
    echo multi();
    break;

  case "/":

  echo divide();
  break;

  default:
  echo "Require an answer";
  }
}

?>