根据下拉列表的选择提交来自不同字段的数据 - laravel

Submitting data from different fields depending on selection of dropdown - laravel

我的表单包含一个下拉菜单,可以选择是或否。如果选择是,将出现一个下拉列表。选择否时,将出现输入文本字段。但是,此功能有效,但我无法提交数据。我只能在删除 "Dropdown 2" 或 "input 1" 之一时提交我的数据(请参阅我指的是哪个字段的代码)。

如果我提交数据,我会收到以下错误 htmlspecialchars() expects parameter 1 to be string, array given。我认为这是因为我有两个可以发送数据的字段。这两个字段(隐藏和显示字段)然后将数据作为数组发送,而不是从显示的字段向控制器发送数据。这怎么能解决。有人可以帮助我吗?

谢谢

function displayField() {
      if (document.getElementById("isYes").selected) {
        document.getElementById("ifyes").style.display = "block";
        document.getElementById("check").style.display = "block";
        document.getElementById("ifno").style.display = "none";
        document.getElementById("ifyes").required = true;
      } else if (document.getElementById("isNo").selected) {
        document.getElementById("ifno").style.display = "block";
        document.getElementById("check").style.display = "block";
        document.getElementById("ifyes").style.display = "none";
        document.getElementById("ifno").required = true;
      } else {
        document.getElementById("ifno").style.display = "none";
        document.getElementById("ifyes").style.display = "none";
        document.getElementById("check").style.display = "none";
      }
    };
<form action="/tests" method="POST">

<div class="row">
  <div class="col-2">
    <div class="d-flex">
      <div class="p-1">
        <label class="p-2" for="type">Type </label>
      </div>
    </div>
  </div>
  <div class="col-4">
    <div class="d-flex">
      <div class="flex-fill p-2">
        <select name="type" class="form-control" onchange="displayField()" required>
          <option></option>
          <option id="isYes">Yes</option>
          <option id="isNo">No</option>
        </select>
      </div>
    </div>
  </div>

  <div class="col-2">
    <div class="d-flex">
      <div class="p-1">
        <label id="check" style="display: none;" class="p-2" for="select">Select</label>
      </div>
    </div>
  </div>
  <div class="col-4">
    <div class="d-flex">
      <div class="flex-fill p-2">
# Dropdown 2
        <select name="select" class="form-control" id="ifyes" style="display: none;" >


          <option>IC</option>
          <option>PD</option>
          <option>RB</option>
          <option>VDQ</option>
          <option>VDS</option>
        </select>
# input 1
        <input style="display: none;" id="ifno" type="text" class="form-control input-text" placeholder="select" name="select">
      </div>
    </div>
  </div>
</div> 
<div class="row">
  <div class="col-4 offset-4">
    <button type="submit" class="btn btn-primary btn-block" style="margin: 10px;">New </button>
  </div>
</div>  
</form>

I have also the same problem that form is not submit. but using below code 
this works well for me.You should change according to your code.    
<!-- This is my html code -->
 <select id="imA" name="imA" onchange="addfield();" required="">
                <option value="">I am a ...</option>
                <option value="teacher">Teacher</option>
                <option value="student">Student</option>
 </select>

 <div id="showField">
 </div>

 <script type="text/javascript">
function addfield(){
    var evalue = $('#imA').val();
    if(evalue == 'student'){            
        $('#showField').html('<input type="text" placeholder="  Enrollment 
 No." name="eno" id="eno" required>');
    }
    else{
        $('#showField').html('');
    }      

}

此问题的解决方案是在隐藏字段时禁用字段。因此您可以使用

document.getElementById('id_of_field_to_disable').disabled = true

这里有类似问题的解决办法。

此外,如果您的字段是必需的,请检查您的控制器的验证。