简体   繁体   English

验证时遇到问题<select>一个更大的元素<form>element - 它不会返回错误消息

[英]Having trouble validating the <select> element within a larger <form> element - it won’t return an error message

I've been building a series of webpages that allows for dynamic data manipulation (CRUD).我一直在构建一系列允许动态数据操作 (CRUD) 的网页。 The pages are built with HTML, CSS, and PHP.这些页面是用 HTML、CSS 和 PHP 构建的。 At this stage of the project, my goal is to have the site return an error message if data is not entered into a particular field.在项目的这个阶段,我的目标是如果数据未输入到特定字段中,则让站点返回错误消息。 I've made a small sandbox for the problem.我为这个问题做了一个小沙箱。

At the top of the page, I included the following PHP logic to outline the variables and how the page will behave if data is entered into the form fields (or not entered):在页面的顶部,我包含了以下 PHP 逻辑来概述变量以及如果将数据输入到表单字段(或未输入)页面将如何表现:

$routeType = "";
$hasRouteType = false;
$routeTypeError = false;

if( isset($_POST["add"]) ) {

   if( isset($_POST['route-type']) ) {
      $routeType = $_POST['route-type'];

      if($routeType) {
         $hasRouteType = true;
      } else {
         $routeTypeError = "Please select a route type.";
      }
   }
}

I made a dropdown menu as one of the elements in the form as follows:我制作了一个下拉菜单作为表单中的元素之一,如下所示:

<form method='POST'>
    <field>
        <label>Route Type</label>
        <select name="route-type" id="route-type">

            <option value="" selected="true" disabled="disabled">What type of route is this?</option>
            <option value="interstate">Interstate</option>
            <option value="stateRoute">State Route</option>

            <?php if($routeTypeError) { ?>
                <p class='error'><?=$routeTypeError?></p>

            <?php } ?>
        </select>
    </field>
<form>

<button class='route-button' type="submit" name='add'>Add route</button>

At this stage, when the user doesn't choose anything from the dropdown, no error message is returned -- again, this is the goal of this stage of the project.在这个阶段,当用户没有从下拉列表中选择任何东西时,不会返回任何错误消息——这也是项目这个阶段的目标。

Steps I've tried to solve the problem so far (these didn't work)到目前为止我试图解决问题的步骤(这些没有用)

I've tried changing the logic in the principal foreach statement.我尝试更改主要 foreach 语句中的逻辑。

if($routeType) {
$hasRouteType = true;

if($routeType != "") {
 $hasRouteType = true;

I've tried changing the data type of the $routeType variable when it's initialized.我尝试在$routeType变量初始化时更改它的数据类型。

I've tried adding selected=“true” and disabled=“disabled” to the top element in the dropdown menu to make it the first thing that appears on the page — and also to make it impossible for the user to select it as an option from the menu.我尝试将 selected=“true” 和 disabled=“disabled” 添加到下拉菜单中的顶部元素,以使其成为页面上出现的第一件事——同时也让用户无法选择它作为菜单中的选项。

<option value="" selected="true" disabled="disabled">What type of route is this?</option>

I've been told that "looping through the option elements" might be a possible solution, but I don't know how to implement this.我被告知“遍历选项元素”可能是一个可能的解决方案,但我不知道如何实现这一点。

My goal is to have the error message "Please select a route type."我的目标是显示错误消息“请选择路线类型”。 returned if the user does not select anything from the dropdown menu.如果用户没有从下拉菜单中选择任何内容,则返回。

Thanks very much for the help!非常感谢您的帮助!

You need an option field with empty value and the required .您需要一个具有空值required的选项字段。

  1. Solution with client-side check带有客户端检查的解决方案
<select name="route-type" id="route-type" required="required">
 <option value="">What type of route is this?</option>
 <option value="A" >Select A</option>
 <option value="B" >Select B</option>
</select>

You don't need php for this.你不需要 php。

Try to submit this without select a value and you will get an error message.尝试在不选择值的情况下提交此内容,您将收到一条错误消息。

All html and javascript checks are just goodies, you always have to check all values ​​with php(when you use php) when you get them from a form.所有 html 和 javascript 检查都只是好东西,当您从表单中获取它们时,您总是必须使用 php(当您使用 php 时)检查所有值。

Never trust any data and check every data you receive!永远不要相信任何数据并检查您收到的每一个数据!

UPDATE:更新:

Maybe this is a possible solution you looking for with php:也许这是您使用 php 寻找的可能解决方案:

  1. Solution with php check使用 php 检查的解决方案
<?php
 
   $route_check = array(
        'interstate'
       ,'stateRoute'
    );
    
    if(!in_array($_POST['route_type'] ?? '' , $route_check)){

?>

<form method="post">
    <select name="route_type">
        <option value="">SELECT A TYPE</option>
        <option value="interstate">interstate</option>
        <option value="stateRoute">stateRoute</option>
    </select>
    <input type="submit">
</form>

<?php

    }
    else{
        echo "DO SOMETHING";
    }
?>

If you wan't to check with the required option also on the client-side use:如果您不想在客户端使用所需的选项进行检查:

<select name="route_type" required="required">

You can also build your option fields with a loop and use the values from the array.您还可以使用循环构建选项字段并使用数组中的值。

  1. Solution with php check and custom error message(javascript alert):使用 php 检查和自定义错误消息(javascript 警报)的解决方案:
<?php
 
   $route_check = array(
        'interstate'
       ,'stateRoute'
    );
    
    if(!in_array($_POST['route_type'] ?? '' , $route_check)){
      if( ($_POST['SEND'] ?? '') === 'SEND'){
        echo "<script>alert('Please select type')</script>";
       }
?>

<form method="post">
    <select name="route_type">
        <option value="">SELECT A TYPE</option>
        <option value="interstate">interstate</option>
        <option value="stateRoute">stateRoute</option>
    </select>
    <input type="submit" name="SEND" value="SEND">
</form>

<?php

    }
    else{
        echo "DO SOMETHING";
    }
?>

  1. Solution with php check and custom error message(php in option field):使用 php 检查和自定义错误消息的解决方案(选项字段中的 php):
<?php
 
   $route_check = array(
        'interstate'
       ,'stateRoute'
    );
    
    if(!in_array($_POST['route_type'] ?? '' , $route_check)){
      if( ($_POST['SEND'] ?? '') === 'SEND'){
        $MESSAGE = 'NO TYPE SELECTED';
       }
       else{
       $MESSAGE = 'SELECT A TYPE';
      }
?>

<form method="post">
    <select name="route_type">
        <option value=""><?php echo $MESSAGE; ?></option>
        <option value="interstate">interstate</option>
        <option value="stateRoute">stateRoute</option>
    </select>
    <input type="submit" name="SEND" value="SEND">
</form>

<?php

    }
    else{
        echo "DO SOMETHING";
    }
?>

This is just a small example of how it could work, of course you can further optimize and adapt the code这只是它如何工作的一个小例子,当然您可以进一步优化和调整代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM