【问题标题】:Taking input in PHP from checkbox in loop从循环中的复选框获取 PHP 中的输入
【发布时间】:2020-04-12 20:08:21
【问题描述】:
<?php

if(isset($_POST['submit'])){
    $field_Values_array = $_POST['field_name'];
    $pass_array = $_POST['class'];

    $sg = "";

    $len1 = count($field_Values_array);
    $class = implode(", ",$pass_array);

    $x=0;
    while($x<$len1){
        $sg = $sg."Book - ".$field_Values_array[$x]." Set - ".$class."<br>";
        $x++;
    }

    echo $sg;

    print '<pre>' ; print_r($field_Values_array); print '</pre>';
    print '<pre>' ; print_r($pass_array); print '</pre>';
}

?>


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    var maxField = 10; //Input fields increment limitation
    var addButton = $('.add_button'); //Add button selector
    var wrapper = $('.field_wrapper'); //Input field wrapper
    var fieldHTML = '<div><input type="text" name="field_name[]" value=""/> <input type="checkbox" name="class[]" value="A" />A <input type="checkbox" name="class[]" value="B" />B <input type="checkbox" name="class[]" value="C" />C <input type="checkbox" name="class[]" value="1" />1 <input type="checkbox" name="class[]" value="2" />2 <input type="checkbox" name="class[]" value="3" />3 <input type="checkbox" name="class[]" value="4" />4 <input type="checkbox" name="class[]" value="5" />5 <input type="checkbox" name="class[]" value="6" />6 <input type="checkbox" name="class[]" value="7" />7 <input type="checkbox" name="class[]" value="8" />8 <a href="javascript:void(0);" class="remove_button"><img src="remove.png"/></a></div>'; //New input field html 
    var x = 1; //Initial field counter is 1

    //Once add button is clicked
    $(addButton).click(function(){
        //Check maximum number of input fields
        if(x < maxField){ 
            x++; //Increment field counter
            $(wrapper).append(fieldHTML); //Add field html
        }
    });

    //Once remove button is clicked
    $(wrapper).on('click', '.remove_button', function(e){
        e.preventDefault();
        $(this).parent('div').remove(); //Remove field html
        x--; //Decrement field counter
    });
});
</script>
</head>
<body>
<div class="container">
    <form action="index.php" method="post">
        <div class="field_wrapper">
            <div>
                <input type="text" readonly value="Book Name"/>
                <input type="text" readonly value="Book Set"/>
            </div>
            <div>
                <input type="text" name="field_name[]" value=""/>
                <input type="checkbox" name="class[]" value="A" />A
                <input type="checkbox" name="class[]" value="B" />B
                <input type="checkbox" name="class[]" value="C" />C
                <input type="checkbox" name="class[]" value="1" />1
                <input type="checkbox" name="class[]" value="2" />2
                <input type="checkbox" name="class[]" value="3" />3
                <input type="checkbox" name="class[]" value="4" />4
                <input type="checkbox" name="class[]" value="5" />5
                <input type="checkbox" name="class[]" value="6" />6
                <input type="checkbox" name="class[]" value="7" />7
                <input type="checkbox" name="class[]" value="8" />8
                <a href="javascript:void(0);" class="add_button" title="Add field"><img src="add.png"/></a>
            </div>
        </div><br><input type="submit" name="submit" value="Submit">
    </form>
</div>
</body>
</html>

我希望我的代码就像在第一个输入字段中一样工作. 输出应该像 书籍 - 英语和套装 - A、B、C 书 - 印地语和套装 - 1,2,3,4,5,6,7,8

但我无法得到这个结果。

【问题讨论】:

  • 这能回答你的问题吗? How can I group form elements
  • @MagnusEriksson 如果你能对代码提供一些帮助会更好
  • 我找到了一个更好地解释它的副本。阅读那个答案,你应该明白了。

标签: javascript php arrays loops checkbox


【解决方案1】:

你能试试下面的代码吗,希望对你有帮助

<?php

if(isset($_POST['submit'])){
    $field_Values_array = $_POST['field_name'];
    $pass_array = $_POST['class'];

    $sg = "";

    $len1 = count($field_Values_array);
    $class = implode(", ",$pass_array);

    $x=0;
    while($x<$len1){
        $sg = $sg."Book - ".$field_Values_array[$x]." Set - ".implode(", ",$pass_array[$x])."<br>";
        $x++;
    }

    echo $sg;

    print '<pre>' ; print_r($field_Values_array); print '</pre>';
    print '<pre>' ; print_r($pass_array); print '</pre>';
}

?>


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<?php 
$index = 0;
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
var index = parseInt('<?php echo $index; ?>');
$(document).ready(function(){
    var maxField = 10; //Input fields increment limitation
    var addButton = $('.add_button'); //Add button selector
    var wrapper = $('.field_wrapper'); //Input field wrapper
    var fieldHTML = '<div><input type="text" name="field_name[indexed]" value=""/> <input type="checkbox" name="class[indexed][]" value="A" />A <input type="checkbox" name="class[indexed][]" value="B" />B <input type="checkbox" name="class[indexed][]" value="C" />C <input type="checkbox" name="class[indexed][]" value="1" />1 <input type="checkbox" name="class[indexed][]" value="2" />2 <input type="checkbox" name="class[indexed][]" value="3" />3 <input type="checkbox" name="class[indexed][]" value="4" />4 <input type="checkbox" name="class[indexed][]" value="5" />5 <input type="checkbox" name="class[indexed][]" value="6" />6 <input type="checkbox" name="class[indexed][]" value="7" />7 <input type="checkbox" name="class[indexed][]" value="8" />8 <a href="javascript:void(0);" class="remove_button"><img src="remove.png"/></a></div>'; //New input field html 
    var x = 1; //Initial field counter is 1

    //Once add button is clicked
    $(addButton).click(function(){
        //Check maximum number of input fields
        index++;

        newHTML = fieldHTML.replace(/indexed/g, index);



        if(x < maxField){ 
            x++; //Increment field counter
            $(wrapper).append(newHTML); //Add field html
        }
    });

    //Once remove button is clicked
    $(wrapper).on('click', '.remove_button', function(e){
        e.preventDefault();
        $(this).parent('div').remove(); //Remove field html
        x--; //Decrement field counter
    });
});
</script>
</head>
<body>
<div class="container">
    <form action="demos.php" method="post">
        <div class="field_wrapper">
            <div>
                <input type="text" readonly value="Book Name"/>
                <input type="text" readonly value="Book Set"/>
            </div>
            <div>
                <input type="text" name="field_name[<?php echo $index; ?>]" value=""/>
                <input type="checkbox" name="class[<?php echo $index; ?>][]" value="A" />A
                <input type="checkbox" name="class[<?php echo $index; ?>][]" value="B" />B
                <input type="checkbox" name="class[<?php echo $index; ?>][]" value="C" />C
                <input type="checkbox" name="class[<?php echo $index; ?>][]" value="1" />1
                <input type="checkbox" name="class[<?php echo $index; ?>][]" value="2" />2
                <input type="checkbox" name="class[<?php echo $index; ?>][]" value="3" />3
                <input type="checkbox" name="class[<?php echo $index; ?>][]" value="4" />4
                <input type="checkbox" name="class[<?php echo $index; ?>][]" value="5" />5
                <input type="checkbox" name="class[<?php echo $index; ?>][]" value="6" />6
                <input type="checkbox" name="class[<?php echo $index; ?>][]" value="7" />7
                <input type="checkbox" name="class[<?php echo $index; ?>][]" value="8" />8
                <a href="javascript:void(0);" class="add_button" title="Add field"><img src="add.png"/></a>
            </div>
        </div><br><input type="submit" name="submit" value="Submit">
    </form>
</div>
</body>
</html>

结果截图如下

【讨论】:

  • 这不起作用 echo $sg;打印'
    '; print_r($field_Values_array);打印'
    ';打印'
    '; print_r($pass_array);打印'
    ';
  • 为什么要使用 PHP 设置 index?为什么不只是var index = 0;?表单也是如此,为什么不直接写:field_name[0]class[0][]
  • @TabishTanseef 我已经修复了第 3 行的问题,请查看编辑后的代码
  • 是的,它现在可以工作了,你能帮我以这种格式打印它吗 echo $sg; { Book - English Set-A,B,C Book - Kavya Set-1,2,3,4 Book - Maths Set-6,7,8 } etc..
  • @TabishTanseef 现在请检查 while 循环,我也修复了打印问题。如果对你有帮助,请点赞并采纳
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-26
  • 2013-04-23
  • 2013-01-28
  • 2013-10-15
  • 1970-01-01
相关资源
最近更新 更多