0

I am just started with Php and have some doubts.

I created 2 pages one.php and two.php

ONE.php
<body>
  <form method="post" action="TWO.php">
    First Number<input type="text" name="txt1"><br>
    Second Number<input type="text" name="txt2"><br>
  <input type="submit">
</form>
</body>

TWO.php
<body>
  <?php
    $sum=$_POST["txt1"] + $_POST["txt2"];
    echo $sum;
  ?>
</body>

I POST values form one.php to two.php. Two.php calculates the sum and echo's the result. My query is would I be able to get the sum echoed on one.php using just php and its important to post the data on another page and get the response from there.

Sumeet Gavhale
  • 800
  • 4
  • 13
  • 39

3 Answers3

1

Yes you are. Simply, handle the form submission (the POST request) in one.php. When the request for one.php is not POST just show the form.

The typical way is:

// ONE.php
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $content = "Sum is " . $_POST["txt1"] + $_POST["txt2"];
}
else {
    $content = <<<EOC
     <form method="post" action="ONE.php">
        First Number<input type="text" name="txt1"><br>
        Second Number<input type="text" name="txt2"><br>
        <input type="submit">
     </form>
EOC;
}
?> 
<!DOCTYPE html>
<html>
<head>
  <title>ONE</title>
  <meta charset="utf-8"> <!-- or whatever charset you are using -->
</head>
<body>
  <?php echo $content ?>
</body>
</html>

Edited, the OP needs the two files but print the result on one.php

In order to pass data between two files you can:

  • Set the data in the first file (two.php) and require the second (one.php, where you would print the value)
  • Use PHP sessions.

With the latter you need to do a (well you don't need to but it's mean to work with a) page redirect, so my recommended approach for this case is use the former. The code could be something like:

// TWO.php
<?php
// you should probably check if $_POST['txt1'] and $_POST['txt2'] does really exists and throw and error if not...
$sum = $_POST["txt1"] + $_POST["txt2"];

require "ONE.php" // careful if you're on a *nix file system the NameCase is extremely important!


// ONE.php
<?php
if (isset($sum)) {
    $content = "Sum is " . $sum;
}
else {
    $content = <<<EOC
     <form method="post" action="TWO.php">
        First Number<input type="text" name="txt1"><br>
        Second Number<input type="text" name="txt2"><br>
        <input type="submit">
     </form>
EOC;
}
?> 
<!DOCTYPE html>
<html>
<head>
  <title>ONE</title>
  <meta charset="utf-8"> <!-- or whatever charset you are using -->
</head>
<body>
  <?php echo $content ?>
</body>
</html>
mTorres
  • 3,590
  • 2
  • 25
  • 36
1

Try this code:

<body>
<?php
if($_POST){
    $sum = $_POST["txt1"] + $_POST["txt2"];
    echo $sum;
}else{
?>
<form method="post" action="">
        First Number<input type="text" name="txt1"><br />
        Second Number<input type="text" name="txt2"><br />
        <input type="submit">
    </form>
<?php } ?>
</body>
1

You can try with this, in only one page:

ONE.php

<html>
<head></head>
<body>
    <form method="post" action="ONE.php">
        First Number<input type="text" name="txt1"><br>
        Second Number<input type="text" name="txt2"><br>
       <input type="submit">
    </form>
    <?php
        // Verify if $_POST["txt1"] and $_POST["txt1"] are defined
        // (when form is submit $_POST, $_GET and other $_ PHP vars 
        // are set). If form isn't submitted, set 0 on each variable 
        // to perform sum. Is necessary check values to avoid PHP
        // Warnings/Errors (In this case with isset function. There
        // are many different ways to perform it)
        $txt1 = isset($_POST["txt1"]) ? $_POST["txt1"] : 0;
        $txt2 = isset($_POST["txt2"]) ? $_POST["txt2"] : 0;
        $sum = $txt1 + $txt2;
        //Print sum result
        echo 'Sum is: '.$sum;
    ?>
</body>
</html>

For comparations I'm using ternary operators, that simplify/minimize code of traditional if/else. Also you can use traditional if/else (simple compare) or switch(multiple compare)

Community
  • 1
  • 1
Alejandro Quiroz
  • 2,604
  • 1
  • 15
  • 17