【问题标题】:PHP - Multiselect require field if option is selected [closed]PHP - 如果选择了选项,则多选需要字段[关闭]
【发布时间】:2013-01-14 17:12:03
【问题描述】:

这是通过电子邮件发送的表单的一部分。当您选择选项状态时,文本字段状态是必需的,如果您不这样做,则县和邮政编码相同。 我想要的是 php 来验证这一点,以便我可以发送电子邮件,但到目前为止我还无法完成它。

这是 HTML:

<select multiple="multiple" name="geographic" id="geographic">
    <option value="state">State</option>
    <option value="county">County</option>
    <option value="zip">Zip Code</option>
</select>
<label for="state">What state?</label>
<input id="state" name="state" type="text">
<label for="county">What County?</label>
<input id="county" name="county" type="text">
<label for="zipcode">What is the Zip Code? *</label>
<input id="zipcode" name="zipcode" type="text">

【问题讨论】:

  • 您不需要在服务器上进行此验证。为什么不改用 JavaScript?

标签: php multi-select required


【解决方案1】:

您可以使用Javascript,它允许在前端进行验证并且不需要页面加载,如下所示应该适合您:

<head>
<script type="text/javascript">
function SelectValidator() {
  // Get the select object
  var index = document.getElementById("geographic").selectedIndex;
  var select = document.getElementById("geographic").options;

  // Get our input elements
  var county = document.getElementById('county');
  var state = document.getElementById('state');
  var zipcode = document.getElementById('zipcode');

  if (select[index].value === "county") {
    if (county.value === null || county.value === "") {
      alert("Please complete the County field");
    } else {
      alert("You could simply put a return statement here.");
    }
  } else if (select[index].value === "state") {
    if (state.value === null || state.value === "") {
      alert("Please complete the state field");
    } else {
      alert("You could simply put a return statement here.");
    }
  } else if (select[index].value === "zip") {
    if (zipcode.value === null || zipcode.value === "") {
      alert("Please complete the Zip Code field");
    } else {
      alert("You could simply put a return statement here.");
    }
  }
}
</script>
</head>

还有你的表格:

<form>
<select multiple="multiple" name="geographic" id="geographic">
    <option value="state">State</option>
    <option value="county">County</option>
    <option value="zip">Zip Code</option>
</select>

<label for="state">What state?</label>
<input id="state" name="state" type="text">

<label for="county">What County?</label>
<input id="county" name="county" type="text">

<label for="zipcode">What is the Zip Code? *</label>
<input id="zipcode" name="zipcode" type="text">


<button type="button" onclick="SelectValidator()">Display index</button>
</form>

【讨论】:

    【解决方案2】:

    首先你们需要将代码封装在一个表单中,但您可能已经这样做了?如果没有,你应该看看http://www.w3schools.com/php/php_forms.asp

    发布表单的 php 文件用 $_POST 接收它,在 geoit 的情况下是 $_POST['geographic'],状态也是如此。现在您知道如何访问该值,您可以对其进行验证。

    所以

    if($_POST['geographic'] == 'state'){
    
         if(empty($_POST['state'])){
              echo 'state is required';
         }else{
              echo 'state is given, proceed';
         }
    
    }
    

    【讨论】:

    • 点击我第一条评论中的链接。阅读它指向的页面。
    猜你喜欢
    • 2019-12-13
    • 2020-05-18
    • 2015-10-08
    • 1970-01-01
    • 2016-12-04
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多