2016-09-22 87 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 />