Messaggio di errore php su input non valido

Sono nuovo di php Sto cercando di printingre un messaggio di errore Se una persona inserisce qualcosa di diverso da un numero reale nel field. Spero che abbia senso ecco il codice, e per favore descrivi come lo hai risolto grazie:

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

 <?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_numbersc($n1)&&is_numbersc($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> 

Quello che stai cercando è la function is_numbersc() .
Date un'occhiata: http://php.net/manual/de/function.is-numbersc.php

usando la function is_numberic () in php puoi verificare se il valore inserito è un numero e se no possiamo restituire un messaggio di errore

 <?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_numbersc($_POST['n1']) && is_numbersc($_POST['n2'])){ $result = $cal->getresult($_POST['n1'],$_POST['n2'],$_POST['op']); }else{ $result="please only insert numbers into the calculator"; } } ?>