简体   繁体   English

表格 - 未转移到 PHP 的清单值

[英]Form - checklist value to not transferred to PHP

I'm trying to populate the multiple selected choice from checklist into PHP.我正在尝试将清单中的多项选择填充到 PHP 中。 However, only the "Reading" choice is populating in the new window and not the other choices that I have selected.但是,只有“阅读”选项填充在新的 window 中,而不是我选择的其他选项。 For example, I have selected "Painting & Travelling" but after I clicked submit, the value that showing is "Reading".Kindly advise how to fix this.例如,我选择了“Painting & Travelling”,但点击提交后,显示的值为“Reading”。请告知如何解决此问题。 Thanks in advance.提前致谢。 I'm new to web development.我是 web 开发的新手。

Javascript Javascript


function validate(){


   var x3=document.getElementById("hobbies");


   if (x3.value == "") 

   {
      alert("No blank values allowed")
      return false;
   }
   else
   {

      window.open('https://quiet-odyssey-258110.appspot.com/?hobbies[]='+x3.value+'','mywin','left=20,top=20,width=500,height=500,toolbar=1,resizable=0');
}

Form形式

<form onsubmit="return validate()" method= "get">



<label>Hobbies : </label><br><br>
<input type="checkbox" id="hobbies" name="hobbies[]" value="Reading"/>Reading<br/>
<input type="checkbox" id="hobbies" name="hobbies[]" value="Painting"/>Painting<br/>
<input type="checkbox" id="hobbies" name="hobbies[]" value="Traveling"/>Traveling<br/>
<input type="checkbox" id="hobbies" name="hobbies[]" value="Baking"/>Baking<br/><br/>


  </div>

</form>

You need to fetch the values of checkbox one by one in loop:您需要在循环中一一获取复选框的值:

<form onsubmit="return validate()" method= "get" name="demoForm">
<label>Hobbies : </label><br><br>
<input type="checkbox" id="hobbies" name="hobbies[]" value="Reading"/>Reading<br/>
<input type="checkbox" id="hobbies" name="hobbies[]" value="Painting"/>Painting<br/>
<input type="checkbox" id="hobbies" name="hobbies[]" value="Traveling"/>Traveling<br/>
<input type="checkbox" id="hobbies" name="hobbies[]" value="Baking"/>Baking<br/><br/>
<button type="submit">click</button>
</form>


<script type="text/javascript">
function validate(){
 var hobby = document.forms['demoForm'].elements[ 'hobbies[]' ];
 var len = hobby.length;
 var values="";

for (var i=0;i<len;i++) 
{
    if (hobby[i].checked) 
    {
        values += hobby[i].value+",";
    }
}
values = values.replace(/,\s*$/, "");
window.open('https://quiet-odyssey-258110.appspot.com/?hobbies[]='+values+'','mywin','left=20,top=20,width=500,height=500,toolbar=1,resizable=0 ');
   }
   </script>

You can also fetch the values using $_GET['hobbies']您还可以使用$_GET['hobbies']获取值

Please try the following:请尝试以下方法:

<form onsubmit="return validate()" method= "get">
    <label>Hobbies : </label><br><br>
    <input type="checkbox" id="hobbies" name="hobbies[]" value="Reading" />Reading<br/>
    <input type="checkbox" id="hobbies" name="hobbies[]" value="Painting" />Painting<br/>
    <input type="checkbox" id="hobbies" name="hobbies[]" value="Traveling" />Traveling<br/>
    <input type="checkbox" id="hobbies" name="hobbies[]" value="Baking" />Baking<br/><br/>
    <input type="submit" value="Submit" />
</form>

<script type="text/javascript">
    function validate() {
      var x3 = document.getElementsByName("hobbies[]");
      var selectedVals = "";

      for(var i = 0; i < x3.length; i++) {
        if((x3[i].checked) && (x3[i].value != '')) {
            selectedVals += (selectedVals == '')?x3[i].value:','+x3[i].value;
          }
      }

      if(selectedVals == "") {
        alert("No blank values allowed")
        return false;
      }
      else {
        window.open('https://quiet-odyssey-258110.appspot.com/?hobbies[]='+selectedVals+'','mywin','left=20,top=20,width=500,height=500,toolbar=1,resizable=0');
      }
    }
</script>

Hope this may help.希望这可能会有所帮助。

You set all elements with the same id id is unique attribute so whatever you select the javascript will always return the first element value on x3 you can do this if you only want to submit selected checkboxes您将所有具有相同 id id 的元素设置为唯一属性,因此无论您使用 select javascript 将始终返回 x3 上的第一个元素值,如果您只想提交选定的复选框,则可以执行此操作

<script type="text/javascript">
    function validate() {
      var x3 = document.getElementsByName("hobbies[]");
      var values= "";

      for(var i = 0; i < x3.length; i++) {
        if(x3[i].checked) {
            if(x3[i].value==''){
                alert('No blank values allowed');
                return false;
            }
            else{
               values+= x3[i].value+',';
            }
        }
     }
     window.open('https://quiet-odyssey-258110.appspot.com/?hobbies[]='+values+'','mywin','left=20,top=20,width=500,height=500,toolbar=1,resizable=0');
        }
    }

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

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