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

Adding a php script into a php web page

ok, so I have create my own local development server using MAMP and when I run my web page I use the following to include my php script.

<?php 

include ("add_up.php");

?>

When my script is run and I return my results in the browser, it then returns the result in the head tags. Only way I can get around this is by doing the following:

<!--<?php 

include ("add_up.php");

?>-->

The php script still gets read but only displays where I want it to, any ideas if this is the correct way of doing it ?

Kind regards,

James

jamesjones21
jamesjones21
9,260 Points

Hi the full code is below:

<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>My Maths Page</title>
    <?php include "add_up.php" ?>
</head>

<body>

    <header> <!-- header start -->
        <nav></nav> <!-- navigaton bar -->
    </header> <!-- header end -->

    <section>
        <h1>Calculations</h1><!-- leave action blank if don't want to be directed to another page -->
        <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>
    </section>

    <footer> <!-- footer start -->
        <div class="date"><?php?></div> <!-- date will go here -->
    </footer> <!-- footer end -->

</body>

</html>

Essentially, I would want to only include my answer where the variable $answer is. But when the script is executed, it displays the result in where <? include "add_up.php" ?> is. So it would display twice, where as I only want to display it once.

jamesjones21
jamesjones21
9,260 Points

php code as follows:

//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";
  }
}

2 Answers

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

The reason why it is displaying in the <head> is because you have included it in the <head>-section.

I have no idea what is inside add_up.php so I can't really tell where to include it, but try to include at the top of the page (above <!DOCTYPE html>) - or if it echo's something then include it where you want it to echo.

Edit: You pasted the add_up.php while I wrote this answer, but in this case I would include it at the very top of the page (above <!DOCTYPE html>)

jamesjones21
jamesjones21
9,260 Points

thanks for the advice, appreciate it, I just tried that now but still displays it above the doc type unfortunately.

Kind regards,

James

jamesjones21
jamesjones21
9,260 Points

thanks for the advice again, I've managed to work it out, as I was returning a the variable $answer from within my functions, there was no point of me echoing them from within my switch statement, therefore it won't be displayed twice and therefore once. :)

I'm still a rookie but I am attempting php each day.

Kind regards,

James

Ahsan Parwez
Ahsan Parwez
3,139 Points

The Solution is simple do not include PHP scripts in <head> tag. Always declare your code on the top of the page and the include function will be like this:

<?php include( 'add_up.php') ?> ?>

jamesjones21
jamesjones21
9,260 Points

Hi Ahsan, thanks for your input, I had to update my add_up.php script to only call the function as I declared the function within my switch statement, and then also echoing my answer variable.

Therefore I removed the echo for $answer and only included the function, but I will take this and Henrik's advice and add my php scripts to the top.

Kind regards,