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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。