2017-02-16 57 views
1

我是新來的PHP我想打印一條錯誤消息如果一個人插入任何其他的字段中的實數。我希望這是有意義這裏是代碼,請描述你是如何解決這得益於:關於無效輸入的PHP錯誤消息

<?php 
$result = ""; 
class calculator 
{ 
var $a; 
var $b; 

function checkopration($oprator) 
{ 
    switch($oprator) 
    { 
     case '+': 
     return $this->a + $this->b; 
     break; 

     case '-': 
     return $this->a - $this->b; 
     break; 

     case '*': 
     return $this->a * $this->b; 
     break; 

     case '/': 
     return $this->a/$this->b; 
     break; 

     default: 
     return "Sorry No command found"; 
    } 
} 
function getresult($a, $b, $c) 
{ 
    $this->a = $a; 
    $this->b = $b; 
    return $this->checkopration($c); 
} 
} 

$cal = new calculator(); 
if(isset($_POST['submit'])) 
{ 
    $result = $cal->getresult($_POST['n1'],$_POST['n2'],$_POST['op']); 
} 
?> 

    <form method="post"> 
    <table align="center"> 
    <tr> 
     <td><strong><?php echo $result; ?><strong></td> 
    </tr> 
    <tr> 
      <td>Enter 1st Number</td> 
    <td><input type="text" name="n1"></td> 
</tr> 

<tr> 
    <td>Enter 2nd Number</td> 
    <td><input type="text" name="n2"></td> 
</tr> 

<tr> 
    <td>Select Oprator</td> 
    <td><select name="op"> 
     <option value="+">+</option> 
     <option value="-">-</option> 
     <option value="*">*</option> 
     <option value="/">/</option> 
    </select></td> 
</tr> 

<tr> 
    <td></td> 
    <td><input type="submit" name="submit" value="Submit"></td> 
</tr> 

</table> 
</form> 

回答

1
<?php 
$result = ""; 
class calculator 
{ 
    var $a; 
    var $b; 

    function checkopration($oprator) 
    { 
     switch($oprator) 
     { 
     case '+': 
      return $this->a + $this->b; 
      break; 

     case '-': 
      return $this->a - $this->b; 
      break; 

     case '*': 
      return $this->a * $this->b; 
      break; 

     case '/': 
      return $this->a/$this->b; 
      break; 

     default: 
      return "Sorry No command found"; 
     } 
    } 
    function getresult($a, $b, $c) 
    { 
     $this->a = $a; 
     $this->b = $b; 
     return $this->checkopration($c); 
    } 
} 

$cal = new calculator(); 
if(isset($_POST['submit'])) 
{ 
    $n1=$_POST['n1']; 
    $n2=$_POST['n2']; 
    $op=$_POST['op']; 
    if(is_numeric($n1)&&is_numeric($n2)){ 
     $result = $cal->getresult($n1,$n2,$op); 
    }else{ 
    $result="<p style='background-color:#FFCCCC'><b>ERROR: </b>Invalid input</p><br>"; 
    } 
} 
?> 

    <form method="post"> 
    <table align="center"> 
    <tr> 
     <td><strong><?php echo $result; ?><strong></td> 
    </tr> 
    <tr> 
      <td>Enter 1st Number</td> 
    <td><input type="text" name="n1"></td> 
</tr> 

<tr> 
    <td>Enter 2nd Number</td> 
    <td><input type="text" name="n2"></td> 
</tr> 

<tr> 
    <td>Select Oprator</td> 
    <td><select name="op"> 
     <option value="+">+</option> 
     <option value="-">-</option> 
     <option value="*">*</option> 
     <option value="/">/</option> 
    </select></td> 
</tr> 

<tr> 
    <td></td> 
    <td><input type="submit" name="submit" value="Submit"></td> 
</tr> 

</table> 
</form> 
+0

感謝iqbal_cs – maria

0

使用在PHP中is_numberic()函數,您可以檢查是否在輸入的值是一個數字,如果沒有,我們可以返回一個錯誤消息

<?php 
$result = ""; 
class calculator 
{ 
var $a; 
var $b; 

function checkopration($oprator) 
{ 
    switch($oprator) 
    { 
     case '+': 
     return $this->a + $this->b; 
     break; 

     case '-': 
     return $this->a - $this->b; 
     break; 

     case '*': 
     return $this->a * $this->b; 
     break; 

     case '/': 
     return $this->a/$this->b; 
     break; 

     default: 
     return "Sorry No command found"; 
    } 
} 
function getresult($a, $b, $c) 
{ 
    $this->a = $a; 
    $this->b = $b; 
    return $this->checkopration($c); 
} 
} 

$cal = new calculator(); 
if(isset($_POST['submit'])) 
{ 
    if(is_numeric($_POST['n1']) && is_numeric($_POST['n2'])){ 
     $result = $cal->getresult($_POST['n1'],$_POST['n2'],$_POST['op']); 
    }else{ 
     $result="please only insert numbers into the calculator"; 
    } 
} 
?>