Show result on same page with php

Asked

Viewed 1,706 times

0

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

Follow the structure of my project:

inserir a descrição da imagem aqui

My codes:

index php.:

<?php include "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 recover the values in index php. with the file Calc.php. And in the archive Calc.php I pass the values to the Calc.inc.php so that the calculations are performed and the final value is returned to the Calc.php. The problem is that in method is redirected to the page Calc.php and the result is displayed on this other page, I’d like it to appear on the same page as index.php.

I tried to do something with Header("Location: index.php") but this causes the page to update, so it didn’t solve. Is it possible to do what I want? I accept different types of solutions!

  • You can check if the POST, if it is POST just display the result.

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 whether 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 don’t forget to switch the action to index.php

<form action="index.php" method="POST">
  • of course it’s quite simple, it was more to give you the logic.

  • Your proposal has worked! However, error messages appeared on the index.php page: Notice: Undefined index: num1 in C: xampp htdocs... Calc.php on line 6 Notice: Undefined index: num2 in C: xampp htdocs... Calc.php on line 7 Notice: Undefined index: cal in C: xampp htdocs... Calc.php on line 8 Error ?

  • I set! no if(isset($_POST)){} failed to put the name of what I would like to check! And also the name of the button was missing! Then in header.php on <button type="Submit">Calculate</button> the name="Submit" property was added... Final result: <button type="Submit" name="Submit">Calculate</button> and no if: if (isset($_POST['Submit'])) {} and the final result worked!

  • Good! That’s it. Over time you get the practice and it is automatic test with isset if the field that will be used exists. I ended up not putting them because I focused on the concept only. Flw.

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 header syntax "Location" was used passing the calculation result as the value of the parameter

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

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

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

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

  • I tested it here and it worked out your way! I gave a +1 for her answer because she was useful, yes, but Cleytinho’s answer was the one that most corresponded, so I selected it as correct! But thankful nonetheless... It is an alternative to solve the same problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.