【问题标题】:im lost with validating my code我迷失了验证我的代码
【发布时间】:2016-09-22 17:43:02
【问题描述】:

有人可以帮我验证我的代码吗?用户只能输入数字或整数。 我已经尝试过使用 ctype_digit isumeric 和其他一些,但可能是因为我不太了解如何使用它没有成功

<?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 进行验证。先感谢您 !!

【问题讨论】:

  • 您添加的代码似乎没有尝试任何验证。我没有看到ctype_digitisnumeric。你所说的“没有成功”是什么意思?有错误吗?如果是这样,它们是什么?对于调试问题,您确实需要准确说明您做了什么并描述如何它没有工作。
  • 您想:将用户输入限制为数值,还是:检查用户输入并在值不是数值时警告他?
  • @Don'tPanic 是的,没有验证,因为我什至没有放。当我放置一些我从谷歌搜索中学到的验证代码时,没有什么是成功的。并且因为我没有成功,所以我删除了代码。我的代码在这里没有错误,我唯一想要的是将我的代码验证为数字输入。

标签: php html validation


【解决方案1】:

谢谢大家,对我完成我的代码造成的任何不便深表歉意。

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

【讨论】:

    【解决方案2】:

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

    <?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'; 
       }
    
    }
    
    
    ?>
    

    【讨论】:

    • 也许您应该等一下,看看问题是否可以澄清后再尝试回答?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多