html - 选择未被用户选择的选项 - 验证

时间:2017-04-22 16:10:54

标签: php html drop-down-menu

这是我用于选择城市的脚本。但是用户没有从列表中选择任何内容。

<select>
<option disabled selected value> -- select an option -- </option>
<option>City 1</option>
<option>City 2</option>
<option>City 3</option>
</select>

用户提交表单后,错误就像“通知:第44行/home/.../public_html/address.php中的未定义索引:CITY 列'CITY'不能为空“

如何在提交表单之前对其进行验证?

6 个答案:

答案 0 :(得分:1)

<强> PHP

<?php
$message='';

if(isset($_POST['submit']))
{

    if (!isset($_POST['city']) || $_POST['city'] === '') {
    $message .= " Please Select The City First <br />";
    }
}

if(isset($message))
{
echo '<div style="color:#FF0000;text-align:center;font-size:12px;">'.$message.'</div>';
}

?>

HTML

   <select name="city" selected disabled >
     <option value="city1">City 1</option>
     <option value="city2">City 2</option>
     <option value="city3">City 3</option>
    </select>

答案 1 :(得分:0)

<select name="city">
  <option value="Paris">Paris</option>
  <option value="London">London</option>
</select>

您需要为该选项提供值

答案 2 :(得分:0)

select放置在表单中,添加requirednamevalue属性:

<form>
    <select name="city" required>
        <option disabled selected value> -- select an option -- </option>
        <option value="city1">City 1</option>
        <option value="city2">City 2</option>
        <option value="city3">City 3</option>
    </select>
<input type="submit" value="Send" />
</form>

答案 3 :(得分:0)

您的代码应该是这样的。这样,您的选择将在发送时具有值。并添加&#34; required&#34;将确保用户在提交前必须选择。

&#13;
&#13;
<select name="city" required>
  <option selected disabled>SELECT CITY</option>
  <option value="City 1">City 1</option>
  <option value="city 2">City 2</option>
</select>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

我添加了javascript验证或您的代码。因此它不能为空。在您选择之前,此代码将阻止提交 这是html表单,

 <form action="" method="post" onsubmit="return(Validate());" name="myform">
        <select name="city">
        <option disabled selected value> -- select an option -- </option>
        <option value="City 1">City 1</option>
        <option value="City 1">City 2</option>
        </select>
        <div id="name_error" style="color:red;"></div>
    </form>

这是JS部分

 <script type="text/javascript">
   <!--
      // Form validation code will come here.

      function Validate()
      {

         if( document.myform.city.value == "" )
         {
            alert( "Please provide your city!" );

            name_error.textContent = "City is Required.!"; 
            document.myform.city.focus() ;
            return false;
         }


         return( true );
      }
   //-->
</script>

答案 5 :(得分:0)

您可以使select标签成为必需,并为每个选项传递值。这将完成客户端验证的其余部分

<form method = 'post' action='address.php'>
<select required>
<option value =""> -- select an option -- </option>
<option value ="city1">City 1</option>
<option value ="city1">City 2</option>
<option value ="city1">City 3</option>
</select>
</form>

如果您还想要服务器端验证,则必须在select标记中包含name属性。

<form method ='post' action='address.php'>
    <select name="cities" required>
    <option value =""> -- select an option -- </option>
    <option value ="city1">City 1</option>
    <option value ="city1">City 2</option>
    <option value ="city1">City 3</option>
    </select>
    </form>

并形成名称,您可以获得选择选项的值,并进一步做您想做的事情

$city = $_POST['cities'];