Appear result on same page with php

0

I'm basically starting studies with PHP and what I'm trying to do is simple. I would like to display the result of a calculation on the same page.

The structure of my project follows:

Mycodes:

index.php:

<?phpinclude"header.php"; ?>

<?php include "footer.php"; ?>

header.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<form action="calc.php" method="POST">
    <input type="text" name="num1">
    <input type="text" name="num2">
    <select name="cal" id="">
        <option value="add">Add</option>
        <option value="sub">Subtract</option>
        <option value="mul">Multiply</option>
    </select>
    <button type="submit">Calculate</button>
</form>

calc.php:

<?php

 include 'includes/calc.inc.php';

$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$cal = $_POST['cal'];

$calculator = new Calc($num1, $num2, $cal);
echo $calculator->setCalc(); //gostaria de mostrar na mesma página

calc.inc.php:

<?php

class Calc{
    public $num1;
    public $num2;
    public $cal;

    public function __construct($num1, $num2, $cal){
        $this->num1 = $num1;
        $this->num2 = $num2;
        $this->cal = $cal;
    }

    public function setCalc(){
        switch($this->cal){
            case 'add':
                $result = $this->num1 + $this->num2;
                break;
            case 'sub':
                $result = $this->num1 - $this->num2;
                break;
            case 'mul':
                $result = $this->num1 * $this->num2;
                break;
            default:
                $result = "Error";
                break;
        }
        return $result;
    }
}

I get the values in the index.php with the calc.php file. And in the file calc.php I pass the values to the calc.inc.php so that in this the calculations are performed and the final value for calc.php is returned. The problem is that in the method > is redirected to the calc.php page and the result is displayed on this other page, being that I would like it to appear on the same index.php page.

I tried to do something with Header ("Location: index.php") but this causes the page to refresh, so it did not work. Is it possible to do what I want? I accept different types of solutions!

    
asked by anonymous 13.12.2017 / 00:33

2 answers

1

In your index add calc.php

<?php include "header.php"; ?>
<?php include "calc.php"; ?>    
<?php include "footer.php"; ?>

In calc.php check if a post has been sent.

<?php
if (isset($_POST)) {
  include 'includes/calc.inc.php';

  $num1 = $_POST['num1'];
  $num2 = $_POST['num2'];
  $cal = $_POST['cal'];

  $calculator = new Calc($num1, $num2, $cal);
  echo $calculator->setCalc();
}
?>

And do not forget to change the action to index.php

<form action="index.php" method="POST">
    
13.12.2017 / 01:08
1

calc.php

include 'includes/calc.inc.php';

$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$cal = $_POST['cal'];

$calculator = new Calc($num1, $num2, $cal);
$result = $calculator->setCalc(); //gostaria de mostrar na mesma página

header("Location: index.php?cal=$result");

header.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<form action="calc.php" method="POST">
    <input type="text" name="num1">
    <input type="text" name="num2">
    <select name="cal" id="">
        <option value="add">Add</option>
        <option value="sub">Subtract</option>
        <option value="mul">Multiply</option>
    </select>
    <button type="submit">Calculate</button>
</form>

<?php
  if (isset($_GET["cal"])){
      echo $_GET["cal"];
  }
?>
  

In calc.php the "Location" header syntax was used, passing the result of the

header("Location: index.php?cal=$result");
  

In header.php we retrieve this value to appear in index.php

<?php
  if (isset($_GET["cal"])){
      echo $_GET["cal"];
  }
?>

In fact, the code snippet immediately above does not have to be included in Na header.php , can be included in index.php or footer.php , thus giving the opportunity to place the result in the exact place wherever it appears inside the page.

    
13.12.2017 / 13:25