donglang7236 2014-05-13 07:55
浏览 76
已采纳

在PHP中验证并提交数组中的多个输入字段:排名顺序

I am using PHP to generate an HTML form for a ranking task, in order to obtain the rank order that the user assigns to a list of alternatives. The generated form is presented as a <table> in which the rows containing the alternatives are randomised. The generated form HTML will be something like (for instance):

<table>
    <tbody>
        <tr>
            <th align="Left">Action</th>
            <th>Rank</th>
        </tr>
        <tr>
            <td>
                <input id="alt_ord_2" type="hidden" value="1" name="alt_ord[]">
                <input id="alt_id2" type="hidden" value="2" name="alt_id[]">
                    Alternative 2 text
            </td>
            <td align="center">
                <input id="rankid_[2]" type="text" size="1" maxlength="1" name="rank_[2]">
            </td>
        </tr>
        <tr>
            <td>
                <input id="alt_ord_1" type="hidden" value="2" name="alt_ord[]">
                <input id="alt_id1" type="hidden" value="1" name="alt_id[]">
                    Alternative 1 text
            </td>
            <td align="center">
                <input id="rankid_[1]" type="text" size="1" maxlength="1" name="rank_[1]">
            </td>
        </tr>
        <tr>
            <td>
                <input id="alt_ord_4" type="hidden" value="3" name="alt_ord[]">
                <input id="alt_id4" type="hidden" value="4" name="alt_id[]">
                    Alternative 4 text
            </td>
            <td align="center">
                <input id="rankid_[4]" type="text" size="1" maxlength="1" name="rank_[4]">
            </td>
        </tr>
        <tr>
            <td>
                <input id="alt_ord_3" type="hidden" value="4" name="alt_ord[]">
                <input id="alt_id3" type="hidden" value="3" name="alt_id[]">
                    Alternative 3 text
            </td>
            <td align="center">
                <input id="rankid_[3]" type="text" size="1" maxlength="1" name="rank_[3]">
            </td>
        </tr>
    </tbody>
</table>

So I'm trying to come up with a way to validate the user inputs using PHP, so that the four values entered = [1, 2, 3, 4]; i.e. all entered values are not empty, unique integers between 1 and 4 (the rank order). I am using $_POST to submit the form.

I'm new to this, so any help would be greatly appreciated.


Edit: here is the PHP code used to generate the form.

$rand_ord = "SELECT * FROM altrank ORDER BY rand()";
$result = $link->query($rand_ord) or die(mysql_error($link));

if($result->num_rows > 0){
    $ord = 0;
    echo "<form action='".htmlspecialchars($_SERVER["PHP_SELF"])."' method='post'><table border='1'>
        <tr><th align='Left'>Action</th><th>Rank</th>";
    while ($row = $result->fetch_object()){
        echo '<tr>';
        echo '<td>
            <input type="hidden" name="alt_ord[]" id="alt_ord_'.htmlspecialchars($row->id).'" value="'.++$ord.'">
            <input type="hidden" name="alt_id[]" id="alt_id'.htmlspecialchars($row->id).'" value="'.htmlspecialchars($row->id).'">'.htmlspecialchars($row->alttext).'</td>';
        echo '
            <td align="center"><input type="text" name="rank_[]" id="rankid_['.htmlspecialchars($row->id).']" maxlength=1 size=1></td>';
        echo '</tr>';
        }
    echo "</table><input type='submit' value='Submit' /></form>";
}
else {
    echo "Error: ".$link->error;
}


$link->close();
  • 写回答

1条回答 默认 最新

  • dragonpeng200811111 2014-05-15 05:25
    关注

    I figured out a simple solution that compares the $_POST array against a pre-specified array of accepted values, based on this.

    The validation code:

    $rankErr = "";
    $hasErrors = false;
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $rank = $_POST['rank_'];
    $matchArray = array(1,2,3,4);
        if(is_array($rank) && is_array($matchArray) && array_diff($rank, $matchArray) === array_diff($matchArray, $rank)){
            $hasErrors = false;
        } else {
            $rankErr = "Please enter <b>unique</b> values (1-4) under <em>Rank</em>.";
            $hasErrors = true;
        }
        if (!$hasErrors) {
        include ("process.php"); 
        }
    }
    

    Seems to work perfectly. Hope this helps others with the same problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘