input
input

Reputation: 7519

Passing result values from one PHP page to another

I've a form in which I send the values to be calculated to another PHP page which should do the calculation and return the values to this form page. If there is an error, it should return the error to this main page. How would I do that?

This is the basic form page: index.php:

<form id="deps" name="deps" action="calc.php" method="POST"> 
                <div id="er"> <span class="er" >&nbsp;</span></div>           
                <table class="table">
                    <tr>
                        <td class="field head">&nbsp;</td>
                        <td class="field cv"><label for="met">Metric</label></td>
                    </tr>
                    <tr>
                        <td class="field head"><label for="bd">Bore Dia.</label></td>
                        <td class="field"><input type="text" name="bdmm" id="bdmm" /><label for="bdmm">MM</label></td>
                        <tr>
                    <td class="field head"><label for="tw">Total Weight</label></td>
                    <td class="field"><input type="text" name="twkg" id="twkg" /> <label for="twkg">KG</label></td>

                </tr>
                    </tr>

                 </table>   
                        <input type="submit" id="submit" value="Calculate" />                
              </form>


         </div>

Calc.php

<?php

$bdmm = $_POST['bdmm']; 


if(!$bdmm){ 
          $error = "Please enter a value!";
          echo $error;
     }  
    else {
        echo $bdmm ." success"; 
}
?>

If there is an error it should display the error in the span tag. And if there is no error and it calculates, it should return the result and display it in the textbox.

Is this possible without Javascript?

Upvotes: 0

Views: 771

Answers (1)

SeanCannon
SeanCannon

Reputation: 78016

Sure:

index.php:

<?php
    $err = (isset($_GET['error'])) ? $_GET['error'] : '&nbsp;';
?>
<form id="deps" name="deps" action="calc.php" method="POST"> 
    <div id="er"> <span class="er" ><?php echo $err; ?></span></div>  
    ....rest of form

calc.php:

if(empty($_POST['bdmm'])){ 
    header('Location: index.php?error=Please enter a value!');
    exit();
}  
else {
    echo $_POST['bdmm'] ." success"; 
}

While this works just fine, I'd recommend you simply post the form to the same page and avoid the url redirection. If you use an MVC framework such as CodeIgniter or Zend, you can take advantage of unique urls within the same file and tell the controller to load the appropriate view while keeping the request on the same file.

Upvotes: 2

Related Questions