2011-11-01 72 views
-1

我有一个窗体,我希望计算函数被调用并显示在同一页面上。如果出现错误,它应显示在同一页面上的span标签中。提交并在窗体中显示结果在同一页

这就是我现在所拥有的,而且我遇到了问题:

的index.php [增订]

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

主要有两件事情我想在展示形式:

1-如果有错误,它应在范围标记显示错误 -

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

我这样做了^,但即使文本框为空,它也不会抛出任何错误。

2 - 我想显示在窗体本身的文本框计算结果:

<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; ?>" /> 

^这是抛出一个错误:Undefined variable nbnm and twkg

我要去哪里错了?

回答

1

您的代码有几个问题。

首先,在Calc类中建立的变量不会被类声明以外的代码直接访问。您的代码<?php echo $twkg; ?>由于范围而不起作用 - 变量twkg在您输出HTML的全局范围中不存在。

您可以在Calc类中访问这些变量,但您已将它们设置为私有。如果这些变量保持私有状态(因此<?php echo $calc->getTwkg(); ?>)或者使它们公开并使用箭头运算符(因此<?php echo $calc->twkg; ?>)访问它们,您必须为这些变量创建一个getter方法。

至于错误信息,正如已经指出的那样,邮政处理代码需要超过表单呈现代码,否则表单将在较低代码有机会决定是否存在错误与否。其次,我不确定$_REQUEST['error']的用途是什么:将$error设置为false,检查错误,如果有错误,则将错误消息粘贴在其中。然后你如果看起来像这样:

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

下面是一些一般的编辑...你已经得到了很多混乱的东西在里面,我不知道你在做什么,所以我只是把这个一起以一组技巧的方式。我建议你使用更多的描述性变量名称:而不是$nob$bdmn,使用$bore_diameter_minimum - 这样很容易看出变量应该包含什么。

// 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

谢谢你的解释。我试图实现你的代码,但它会抛出这个错误:'未定义的变量:错误'。我不知道为什么。我做了一个新的PHP页面,只是复制了HTML和错误部分,但它给了我'未定义'错误。 – input

+0

哎呦,看编辑。在if语句上移动'$ error = false;'行。 –

0

您需要将php代码移动到页面顶部,因为即使在创建它们之前您也正在访问变量。

+0

好的,我做到了,但仍然没有输出和相同的问题。 – input

0
  1. 你需要把$错误莫名其妙地到$ _REQUEST(因为你试图从那里得到它,它会自动不存在)。你应该使用$ _SESSION ['error']而不是$ error =“Error!”如果(isset($ _ SESSION ['error'])){而不是if(isset($ _ REQUEST ['error'])){

  2. 将所有代码从底部移到顶部。我没有仔细研究代码,但似乎脚本没有机会在变量设置之前设置变量。

+0

1.我用SESSION但没有成功。 2.我将所有代码从底部移到顶部,但仍然是相同的错误。 – input

相关问题