doujiu3097 2011-07-16 15:28
浏览 58
已采纳

如何在用户更改firebug中输入元素的名称时验证表单?

I realize it's impossible to prevent someone from changing the names of input elements in firebug.

How should I approach this problem? A user changes the name of the input element "firstname" to "month" and visa versa.

<form action="example.php" method="post" enctype="multipart/form-data">
    <table  border='2'>
        <tr>
            <td>
                First name: <input type="text" name="firstname" /><br />
            </td>
        </tr>
        <tr>
            <td>
                Last name: <input type="text" name="lastname" /><br />
            </td>
        </tr>
        <tr>
            <td>Birth Month:
                <select name="month">
                    <option value="01">January</option>
                    <option value="02">February</option>
                    <option value="03">March</option>
                    <option value="04">April</option>
                    <option value="05">May</option>
                    <option value="06">June</option>
                    <option value="07">July</option>
                    <option value="08">August</option>
                    <option value="09">September</option>
                    <option value="10">October</option>
                    <option value="11">November</option>
                    <option value="12">December</option>
                </select>
                <br />
            </td>   
        </tr>
        <tr>
            <td><input type="submit" name="Submit" value="Sign Up!" /></td>
        </tr>
    </table>
</form>

My best idea so far is:

<?php
$month= $_POST['month'];
if($month!= 01 || $month!= 02 ... $month!= 12)
    echo 'wrong month';
?>

However this won't be clean for the year of birth... Facebook does a great job of preventing this when you sign up but I wasn't able to figure out what they did. A non-javascript solution would be much appreciated.

EDIT: Lawrence, what do you recommend for a location form?

  • 写回答

1条回答 默认 最新

  • dsfs5420 2011-07-16 15:49
    关注

    Going through each month in a condition is unneeded, php has functions for that.

    <?php
    $firstname= (string)$_POST['firstname'];
    $lastname = (string)$_POST['lastname'];
    $month    = $_POST['month'];
    
    if(in_array($month,range(01,12))===true){
        $cont = TRUE;
    }else{
        $cont = FALSE;
    }
    //If set, not empty, not swapped for month & greater or equals to 6 chars 
    if(isset($firstname) && $firstname!="" && strlen($firstname) >=6 && in_array($month,range(01,12))===false){
        $cont = TRUE;
    }
    //If set, not empty, not swapped for month & greater or equals to 6 chars
    if(isset($lastname) && $lastname!="" && strlen($lastname) >=6 && in_array($month,range(01,12))===false){
        $cont = TRUE;
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献