Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6759781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:59:27+00:00 2026-05-26T13:59:27+00:00

I’ve a form in which I want the calculate function to be called and

  • 0

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.

Here’s what I have right now, and I’m running into problems:

index.php [UPDATED]:

<?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->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; 
          } 

} 

?> 

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

</head>

<body>

   <!-- Begin Wrapper -->
   <div id="wrapper">


         <div id="column">
              <form id="deps" name="deps" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST"> 
                <?php
                     if (isset($_REQUEST['error'])){     
                        $err = $_REQUEST['error'];
                ?>
                <div id="er"> <span class="er" >&nbsp;<?php echo $err; ?></span></div>  
                <?php  } ?>
                <table class="table">
                    <tr>
                        <td class="field head">&nbsp;</td>
                        <td class="field cv"><label for="met">Metric</label></td>
                        <td class="field cv"><label for="eng">English</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>
                    <tr>
                        <td class="field head"><label for="sd">Screen Dia.</label></td>
                        <td class="field"><input type="text" name="sdmm" id="sdmm" /> <label for="sdmm">MM</label></td>

                    </tr>

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

                    </tr>

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


         </div>

   </div>
   <!-- End Wrapper -->

</body>
</html>

There are mainly two things I want to show in the form:

1- If there is an error, it should display the error in the span tag –

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

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

2- I want to show the calculated results in the textboxes in the form itself:

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

^This is throwing an error: Undefined variable nbnm and twkg

Where am I going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:59:28+00:00Added an answer on May 26, 2026 at 1:59 pm

    There are several problems with your code.

    First, the variables that are established in the Calc class are not going to be directly accessible by code outside of the class declaration. Your code <?php echo $twkg; ?> is not going to work because of scope – the variable twkg does not exist in the global scope where you are outputting the HTML.

    You can access those variables in the Calc class, but you’ve made them private. You will either have to make a getter method for those variables if they remain private (thus <?php echo $calc->getTwkg(); ?>) OR, make them public and access them using the arrow operator (thus <?php echo $calc->twkg; ?>).

    As for the error message, for one, as has been pointed out, the post handling code needs to go above the form rendering code, otherwise the form will be rendered before the lower code has a chance to decide if there’s an error or not. Second, I am not sure what the use of $_REQUEST['error'] is all about: set $error to false, check for errors, if there is one, stick the error message in it. Then your if looks like this:

     if ($error !== false) 
         echo '<div id="er"><span class="er">&nbsp;'.$error.'</span></div>';
    

    Here are some general edits… you’ve got a lot of confusing stuff in there and I don’t know what you’re doing, so I just put this together in the way of a collection of tips. I suggest you use more descriptive variable names: instead of $nob and $bdmn, use $bore_diameter_minimum – that way it is easy to see what a variable should contain.

    // Defining the "calc" class 
    class Calc { 
         private  $vs = 0; 
         public  $nob = 0;  // note that this is public   
         private  $tw = 0; 
    
        public function calculateMet($b,$s) {  
            // do your calculations here
            $this->vs = $b * $s;
    
            // use $this->vs, $this->nob to change the private variables declared above
            if ($this->vs < $v)
            return false;
    
            // return true if the calculation worked
            return true;
        }
    
        // use public getters to return variables that are private
        public function getVs() {
            return $this->vs;
        } 
    
    } 
    $calc = new Calc(); 
    $error = false;
    
    if (isset($_POST['submit'])) {
        $bdmm = isset($_POST['bdmm']) ? $_POST['bdmm'] : false; 
        $sdmm = isset($_POST['sdmm']) ? $_POST['sdmm'] : false; 
    
    
        if(
            $bdmm == false || 
            strlen($bdmm) < 1 || 
            preg_match("/^-?([0-9])+\.?([0-9])+$/", $bdmm) == false
        ){ 
            $error = "Invalid Bore Dia.";
        } 
    
        if(
            $sdmm == false || 
            strlen($sdmm) < 1 || 
            preg_match("/^-?([0-9])+\.?([0-9])+$/", $bdmm) == false
        ){ 
            $error = "Invalid Screen Dia.";
        }
    
        if ($error !== false) {
            $result = $calc->calculateMet($bdmm,$sdmm);
            if ($result === false)
                $error = 'Calculation failed';
        }
    }
    
    // output an error message
    if ($error !== false)
        echo '<div class="error">'.$error.'</div>';
    
    echo 'Private test: '.$calc->getVs();
    echo 'Public test: '.$calc->nob;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a text area in my form which accepts all possible characters from
i want to parse a xhtml file and display in UITableView. what is the
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.