douwu7563 2010-03-18 18:38
浏览 37
已采纳

如何验证PHP表单中的数组?

I'm fairly new to php and was wondering how do I validate the array for the states and where do I place the php code at? I already did the address part but I'm stuck at the states part.

Here is the validation code.

if (isset($_POST['contact_info_submitted'])) { // Handle the form.

    $address = mysqli_real_escape_string(htmlentities($_POST['address']));

}

Here is the form code.

<li><label for="address-1">Address 1: </label><input type="text" name="address-1" id="address-1" size="25" class="input-size" value="<?php if (isset($_POST['address'])) echo $_POST['address']; ?>" /></li>

<li><label for="state-province">State/Province: </label>
<?php
echo '<select name="state-province" id="state-province">' . "
";
    foreach($state_options as $option) {
        if ($option == $state) {
            echo '<option value="' . $option . '" selected="selected">' . $option . '</option>' . "
";
            } else {
              echo '<option value="'. $option . '">' . $option . '</option>'."
";
            }
        }
            echo '</select>';

?>
</li>
<li><input type="submit" name="submit" value="Save Changes" class="save-button" />
    <input type="hidden" name="contact_info_submitted" value="true" />
  • 写回答

1条回答 默认 最新

  • duanhanzi8328 2010-03-18 18:57
    关注

    If you're wondering how to check if someone submitted a valid value for state just check to see if there value they posted is in your array of states. If it's not there it's not valid:

    if (!in_array($_POST['state-province'], $state_options)) {
        // not valid
    }
    

    If you're just trying to store in the a database like the other form values it's no different then any text field:

    if (isset($_POST['contact_info_submitted'])) { // Handle the form.
        $state= mysqli_real_escape_string($_POST['state-province']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化