简体   繁体   English

如何添加具有相同文本输入名称和单选按钮(html、php)的多个条目

[英]how to add multiple entries having the same text input name and radio buttons (html,php)

<form action="confirm.php" method="post" name="">
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF  
<br>
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF  
<br>
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF  
<br>
<br>
<button type="submit" class="">Submit</button>
</form>

having problem with the radio buttons.单选按钮有问题。

And on the confirm page I have used foreach loop.在确认页面上,我使用了 foreach 循环。 How do i also get the values for "f_status" ?我如何获得“f_status”的值?

See first of all its an interesting question but unfortunately, the fact is HTML can't understand the field without different names if they are in same form.首先看看它是一个有趣的问题,但不幸的是,事实是如果它们的形式相同,HTML 无法理解没有不同名称的字段。

so the only way to achieve your goal is to put all of them in three different form tags and then u can name all of the same ie f_hobby[]所以实现你的目标的唯一方法是将它们全部放在三个不同形式的标签中,然后你可以命名所有相同的标签,即f_hobby[]

Also, you need to add a single button to submit all three of them.此外,您需要添加一个按钮来提交所有三个按钮。 To achieve this u can use onsubmit() or onclick() function.为此,您可以使用onsubmit()onclick()函数。

<form action="confirm.php" method="post" name="" id="form1">
    Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
    Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF 
</form> 
<form action="confirm.php" method="post" name="" id="form2">    
    Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
    Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF  
</form>
<form action="confirm.php" method="post" name="" id="form3"> 
    Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
    Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF  
</form> 
<button type="submit" class=""  onclick="submitForms()">Submit</button>

<script>
    submitForms = function(){
        document.getElementById("form1").submit();
        document.getElementById("form2").submit();
        document.getElementById("form3").submit();
        alert("gajab");
    }
</script>

I have given the forms an id to submit it using a single button, u can also use class instead.我已经为表单提供了一个 id 以使用单个按钮提交它,您也可以使用 class 来代替。 I am sure this will solve your problem.我相信这会解决你的问题。

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

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