2016-09-22 39 views
-3

有人可以帮助我验证我的代码吗?用户只能输入数字或整数。 使用ctype_digit ISNUMERIC和其他的一些东西我已经试过了,但可能是因为我没有很了解如何使用它没有理解是成功的即时消失验证我的代码

<?php 
    $table = ''; 

    if ($_POST) { 
     $table .= '<table border="4">'; 
     for ($i = 0; $i < $_POST['rows']; $i++) { 
      $table .= '<tr>'; 
      for ($j = 0; $j < $_POST['column']; $j++) { 
       $table .= '<td width="100">&nbsp;</td>'; 
      } 
      $table .= '</tr>'; 
     } 
     $table .= '</table>'; 
    } 

?> 

<?php echo "A table with "; echo $_POST['rows']; echo " row(s) and "; echo $_POST['column']; echo " column(s) " ;?> 

<?php 
    echo $table; 

?> 


      <form action="" method="post"> 
     <table border="0" width="729"> 
      <tr> 
       <td width="39"><label>Rows :</label></td> 
       <td width="144"><input type="text" name="rows"></td> 

       <td width="51" ><label>Column :</label></td> 
       <td width="352" ><input type="text" name="column"></td> 
      </tr> 

      <tr> 
       <td colspan="2" align="center"><input type="submit" value="Create table"></td> 
      </tr> 
     </table>  

    </form> 
    <br /> 
    <br /> 

的形式应该验证使用PHP的方式。先谢谢你 !!

+1

您添加的代码似乎没有尝试进行任何验证。我没有看到'ctype_digit'或'isnumeric'。你什么意思是“没有成功”?有错误吗?如果是这样,他们是什么?对于调试问题,你真的需要准确地展示你做了什么,并描述_how_它不起作用。 –

+0

是否要:将用户输入限制为数值,或者:检查用户输入并警告他,如果数值不是数值? –

+0

@ Don'tPanic是没有验证,因为我甚至没有放。没有什么是成功的指的是当我把一些验证代码,我从谷歌学习。并且因为我没有成功,所以我删除了代码。我的代码在这里没有错误,我唯一想要的只是将我的代码验证为数字输入。 –

回答

-1

你的问题不是很清楚,但我不知道这样的事情是否适合你。

<?php 
$table = ''; 

if ($_POST) { 
    if(is_numeric($_POST['postVariable'] && is_int($_POST['postVariable']) { 
    $table .= '<table border="4">'; 
    for ($i = 0; $i < $_POST['rows']; $i++) { 
     $table .= '<tr>'; 
     for ($j = 0; $j < $_POST['column']; $j++) { 
      $table .= '<td width="100">&nbsp;</td>'; 
     } 
     $table .= '</tr>'; 
    } 
    $table .= '</table>'; 
    } else { 
     return 'you error message'; 
    } 

} 


?> 
+1

也许你应该等待,看看问题是否可以澄清之前尝试回答? –

0

感谢大家,对于给我带来的任何不便,我很抱歉我完成了我的代码。

<?php 
    $table = ''; 
    $row= $_POST['rows']; 
    $column=$_POST['column']; 
    if (isset ($_POST['submit'])){ 
     if($row=="") 
     $error= "error : please fill out row"; 

     else if($column=="") 
     $error= "error : please fill out column"; 

     elseif(is_numeric($row)==FALSE) 
     $error= "error number only"; 

     elseif(is_numeric($column)==FALSE) 
     $error= "error number only"; 

     else 
     { 

     $table .= '<table border="4">'; 
     for ($i = 0; $i < $_POST['rows']; $i++) { 
      $table .= '<tr>'; 
      for ($j = 0; $j < $_POST['column']; $j++) { 
       $table .= '<td width="100">&nbsp;</td>'; 
      } 
      $table .= '</tr>'; 
     } 
     $table .= '</table>'; 


echo "A table with "; echo $row; echo " row(s) and "; echo $column; echo " column(s) " ; 

    echo $table; 

    } 
    } 
?> 
    <?php 
    if (isset($error)) 
    { 
     echo "<font color='FF0000'><center><b>". $error . 
     "</b></center></font>"; 
    } 
    ?> 

      <form action="" method="post" > 
     <table border="0" width="729"> 
      <tr> 
       <td width="39"><label>Rows :</label></td> 
       <td width="144"><input type="text" name="rows"></td> 

       <td width="51" ><label>Column :</label></td> 
       <td width="352" ><input type="text" name="column"></td> 
      </tr> 

      <tr> 
       <td colspan="2" align="center"><input type="submit" name="submit" value="Create table"></td> 
      </tr> 
     </table>  

    </form> 
    <br /> 
    <br />