Submit and show results in the form in the same page
<p>I've a form in which I want the calculate function to be called and displayed on the same page. If there is an error, it should display in the span tag on the same page. </p>

<p>Here's what I have right now, and I'm running into problems:</p>

<p>index.php <strong>[UPDATED]</strong>:</p>

<pre><code>&lt;?php 

if (isset($_POST['submit'])) {
        $bdmm = $_POST['bdmm']; 
        $sdmm = $_POST['sdmm']; 



        $strings = array(
            $bdmm, 
            $sdmm, 
            $lgpm
        );

            if(!$bdmm){ 
                $error = "Error";
                exit();
            } 
            elseif(!$sdmm){ 
                $error = "Error";
                exit();
            } 


            //check whether the string is numeric
            foreach ($string as $string) {
                if (!preg_match("/^-?([0-9])+\.?([0-9])+$/", $string)) 
                { 
                    echo "Invalid entry. Please enter a number.";  
                } 
            }

                    $calc = new Calc();
            if (isset($bdmm)) {
                $calc-&gt;calculateMet($bdmm,$sdmm);
            }


}

// Defining the "calc" class 
class Calc { 
     private  $vs = 0; 
     private  $nob = 0;     
     private  $tw = 0; 


          public function calculateMet($b,$s)
          {  
                    //code to calculate

            //display nbnm  in textbox  
                    $nbnm = $nobr;

                      //display twkg in textbox    
                    $tw = $nob * 25;
                    $twr = round($tw);
                    $twkg = $tw;

                    exit; 
          } 

} 

?&gt; 

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;Calculator&lt;/title&gt;
&lt;link rel="stylesheet" type="text/css" href="main.css" /&gt;

&lt;/head&gt;

&lt;body&gt;

   &lt;!-- Begin Wrapper --&gt;
   &lt;div id="wrapper"&gt;


         &lt;div id="column"&gt;
              &lt;form id="deps" name="deps" action="&lt;?php echo $_SERVER["PHP_SELF"]; ?&gt;" method="POST"&gt; 
                &lt;?php
                     if (isset($_REQUEST['error'])){     
                        $err = $_REQUEST['error'];
                ?&gt;
                &lt;div id="er"&gt; &lt;span class="er" &gt;&amp;nbsp;&lt;?php echo $err; ?&gt;&lt;/span&gt;&lt;/div&gt;  
                &lt;?php  } ?&gt;
                &lt;table class="table"&gt;
                    &lt;tr&gt;
                        &lt;td class="field head"&gt;&amp;nbsp;&lt;/td&gt;
                        &lt;td class="field cv"&gt;&lt;label for="met"&gt;Metric&lt;/label&gt;&lt;/td&gt;
                        &lt;td class="field cv"&gt;&lt;label for="eng"&gt;English&lt;/label&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td class="field head"&gt;&lt;label for="bd"&gt;Bore Dia.&lt;/label&gt;&lt;/td&gt;
                        &lt;td class="field"&gt;&lt;input type="text" name="bdmm" id="bdmm" /&gt;&lt;label for="bdmm"&gt;MM&lt;/label&gt;&lt;/td&gt;

                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td class="field head"&gt;&lt;label for="sd"&gt;Screen Dia.&lt;/label&gt;&lt;/td&gt;
                        &lt;td class="field"&gt;&lt;input type="text" name="sdmm" id="sdmm" /&gt; &lt;label for="sdmm"&gt;MM&lt;/label&gt;&lt;/td&gt;

                    &lt;/tr&gt;

                    &lt;tr&gt;
                        &lt;td class="field head"&gt;&lt;label for="nbn"&gt;No. of Bags needed&lt;/label&gt;&lt;/td&gt;
                        &lt;td class="field"&gt;&lt;input type="text" name="nbnm" id="nbnm" value="&lt;?php echo $nbnm; ?&gt;" /&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td class="field head"&gt;&lt;label for="tw"&gt;Total Weight&lt;/label&gt;&lt;/td&gt;
                        &lt;td class="field"&gt;&lt;input type="text" name="twkg" id="twkg" value="&lt;?php echo $twkg; ?&gt;" /&gt; &lt;label for="twkg"&gt;KG&lt;/label&gt;&lt;/td&gt;

                    &lt;/tr&gt;

                 &lt;/table&gt;   
                        &lt;input type="submit" id="submit" value="Calculate" /&gt;                
              &lt;/form&gt;


         &lt;/div&gt;

   &lt;/div&gt;
   &lt;!-- End Wrapper --&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>There are mainly two things I want to show in the form:</p>

<p>1- If there is an error, it should display the error in the span tag -</p>

<pre><code>&lt;?php
                         if (isset($_REQUEST['error'])){     
                            $err = $_REQUEST['error'];
                    ?&gt;
                    &lt;div id="er"&gt; &lt;span class="er" &gt;&amp;nbsp;&lt;?php echo $err; ?&gt;&lt;/span&gt;&lt;/div&gt;  
                    &lt;?php  } ?&gt;
</code></pre>

<p>I did this^, but it does not throw any error even if the textbox is blank. </p>

<p>2- I want to show the calculated results in the textboxes in the form itself:</p>

<pre><code>&lt;td class="field"&gt;&lt;input type="text" name="nbnm" id="nbnm" value="&lt;?php echo $nbnm; ?&gt;" /&gt;&lt;/td&gt;
&lt;td class="field"&gt;&lt;input type="text" name="twkg" id="twkg" value="&lt;?php echo $twkg; ?&gt;" /&gt;
</code></pre>

<p>^This is throwing an error: <code>Undefined variable nbnm and twkg</code></p>

<p>Where am I going wrong?</p>
