简体   繁体   English

如果 php 和 jQuery 选中或未选中,则在 php 中获取动态创建的复选框值

[英]getting dynamically created check box value in php if checked or not checked by php and jQuery

I had我有

1 text field 3 check box 1 add more button 1 submit button 1 文本字段 3 复选框 1 添加更多按钮 1 提交按钮

On click of add more button 1 text field and 3 check again added单击添加更多按钮 1 文本字段和 3 再次检查添加

I entered data in text field and checked some of check box我在文本字段中输入了数据并选中了一些复选框

On submit the form ineed.在提交需要的表格。 Data of the text field with corresponding check box value by using jQuery and php使用jQuery和php获取具有对应复选框值的文本字段的数据

The problem with checkbox is that if it is not checked then it is not posted with the form.复选框的问题在于,如果未选中它,则它不会与表单一起发布。 If you check a checkbox and post a form you will get the value of the checkbox in the $_POST variable , if it's unchecked no value will be assigned to the $_POST variable.如果您选中复选框并发布表单,您将在 $_POST 变量中获得复选框的值,如果未选中,则不会为 $_POST 变量分配任何值。 So as to know which checkbox is checked assign a distinct value to it.为了知道哪个复选框被选中,为其分配一个不同的值。

<?php

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

if(isset($_POST['checkbox'])){
echo 'Checkbox: ';
print_r($_POST['checkbox']);
}



if(isset($_POST['text'])){
$text=$_POST['text'];
echo 'Textbox: ';
print_r($text);

}
}
  ?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<form method="POST" action="tests.php">
<input type="checkbox" name="checkbox[]" class="check"  value="1">
<br>
<input type="checkbox" name="checkbox[]" class="check"  value="2">
<br>
<input type="checkbox" name="checkbox[]" class="check"  value="3">
<br>
<input type="text" name="text[]" class="text">
<br>

<br>
<input type="submit" name="submit">
</form>
<button id="add">ADD</button>
</body>
<script>
$(document).ready(function()
{
$('#add').on('click',function()
{
var breaks="<br>"
var checkbox="<br>";
var textbox="<br><input type='text' name='text[]' class='text'>";
var check_length=$('.check').length;

console.log(check_length);
for(var i=check_length+1;i<check_length+4;i++)
{   
    checkbox=checkbox+"<input type='checkbox' name='checkbox[]' class='check' value='" + i + "'><br>";
    

}
$('input[type="text"]').last().after(checkbox,textbox);
});
    });
</script>

</html>

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

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