验证并在阵列中的PHP提交多个输入字段:排序 [英] Validate and submit multiple input fields in array in PHP: rank order

查看:116
本文介绍了验证并在阵列中的PHP提交多个输入字段:排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP来生成排名的任务HTML表单,以获得排名顺序用户分配给替代品清单。生成的表单是psented为&LT $ P $;表> 将含有替代品的行是随机的。生成的HTML表单将是这样(例如):

 <表>
    <&TBODY GT;
        &所述; TR>
            百分位align =left>动作< /第i
            <第i个等级< /第i
        < / TR>
        &所述; TR>
            &所述; TD>
                <输入ID =alt_ord_2类型=隐藏值=1NAME =alt_ord []>
                <输入ID =alt_id2类型=隐藏值=2NAME =alt_id []>
                    方案2的文字
            < / TD>
            < TD ALIGN =中心>
                &所述;输入的id =rankid_ [2]类型=文本大小=1最大长度=1名称=rank_ [2]>
            < / TD>
        < / TR>
        &所述; TR>
            &所述; TD>
                <输入ID =alt_ord_1类型=隐藏值=2NAME =alt_ord []>
                <输入ID =alt_id1类型=隐藏值=1NAME =alt_id []>
                    另类1文本
            < / TD>
            < TD ALIGN =中心>
                &所述;输入的id =rankid_ [1]类型=文本大小=1最大长度=1名称=rank_ [1]>
            < / TD>
        < / TR>
        &所述; TR>
            &所述; TD>
                <输入ID =alt_ord_4类型=隐藏值=3NAME =alt_ord []>
                <输入ID =alt_id4类型=隐藏值=4NAME =alt_id []>
                    方案4文字
            < / TD>
            < TD ALIGN =中心>
                &所述;输入的id =rankid_ [4]类型=文本大小=1最大长度=1名称=rank_ [4]>
            < / TD>
        < / TR>
        &所述; TR>
            &所述; TD>
                <输入ID =alt_ord_3类型=隐藏值=4NAME =alt_ord []>
                <输入ID =alt_id3类型=隐藏值=3NAME =alt_id []>
                    方案3文本
            < / TD>
            < TD ALIGN =中心>
                &所述;输入的id =rankid_ [3]类型=文本大小=1最大长度=1名称=rank_ [3]>
            < / TD>
        < / TR>
    < / TBODY>
< /表>

所以我试图想出一个办法来验证使用PHP的用户输入,使输入=四个值[1,2,3,4]。即所有输入的值不是1和4(排名顺序)之间的空白,唯一的整数。我使用 $ _ POST 来提交表单。

我是新来这个,所以任何帮助将大大AP preciated。


编辑:这里是用来生成形式PHP code

  $ rand_ord =SELECT * FROM altrank ORDER BY兰特();
$结果= $链路>查询($ rand_ord)或死亡(mysql_error($链接));如果($ result-> NUM_ROWS大于0){
    $ ORD = 0;
    回声<形式的行动='的方法='后'&GT用htmlspecialchars($ _ SERVER [PHP_SELF])。'​​;<表边框='1'>
        < TR>百分位ALIGN ='左'>动作< /第i<第i个等级< /第i
    而($行= $ result-> fetch_object()){
        呼应'< TR>';
        呼应'< TD>
            <输入类型=隐藏的名字=alt_ord []ID =_ alt_ord'用htmlspecialchars($行向> ID)。''。​​$ +奥德'值=>
            <输入类型=隐藏的名字=alt_id []ID =alt_id'.htmlspecialchars($行向> ID)。'值='。用htmlspecialchars($行向> ID)。' >'。用htmlspecialchars($行向> alttext)。'< / TD>';
        呼应
            < TD ALIGN =中心><输入类型=文本名称=rank_ []ID =rankid _ ['用htmlspecialchars($行向> ID)。']MAXLENGTH = 1的大小= 1>< / TD>';
        呼应'< / TR>';
        }
    回声< /表><输入类型=提交值=提交/>< /形式为GT;
}
其他{
    回声错误:$链路>错误。
}
$链路>关闭();


解决方案

我想出了一个简单的解决方案是, $ _ POST 阵列针对$ P $比较对指定的接受的值数组的基础上的这个

验证code:

  $ rankErr =;
$ hasErrors = FALSE;如果($ _ SERVER [REQUEST_METHOD] ==POST){
$排名= $ _ POST ['rank_'];
$ matchArray =阵列(1,2,3,4);
    如果(is_array($级)及和放大器; is_array($ matchArray)及和放大器;和array_diff($排名,$ matchArray)===和array_diff($ matchArray,$级)){
        $ hasErrors = FALSE;
    }其他{
        $ rankErr =请输入< B>独有< / B>的值(1-4)在与LT; EM>与排名LT; / EM取代。
        $ hasErrors = TRUE;
    }
    如果(!$ hasErrors){
    包括(process.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();

解决方案

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.

这篇关于验证并在阵列中的PHP提交多个输入字段:排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆