简体   繁体   English

隐藏文本框的发布值

[英]post value of hidden textboxes

i have this code: 我有这个代码:

<?php
$result = mysqli_query($con,"SELECT * FROM table");

while($row = mysqli_fetch_array($result)){
echo "<input type=\"checkbox\" id=\"$row[use_id]\"  name=\"things[]\"  value='$row[col_id]' >$row[col]<br>";
echo "<input placeholder=\"description\" type=\"text\" name=\"ans1\" class='$row[col_id]' id=\"answer1\" style='display:none;'>";
echo "<align='left'><input placeholder=\"source\" type=\"text\" name=\"ans2\" class='$row[use_id]' id=\"answer2\"   style='display:none;'>";

}
?>

using this script: 使用此脚本:

<script>
$(document).ready(function(){


 $("input[type=checkbox]").change(function(){
    var divId = $(this).attr("id");

     if ($(this).is(":checked")){
        $("." + divId).show();
     }
     else{
         $("." + divId).hide();
     }
});
});
</script>

and i want to take the data from the 2 textboxes using ths code: 我想使用此代码从2个文本框中获取数据:

$checkBox = $_POST['things'];


for($i=0; $i<sizeof($checkBox); $i++){


    $qs = "INSERT INTO sccm_reherb.table2 values('$_POST[id]','".$checkBox[$i]."','$_POST[ans1]','$_POST[ans2]')";
    echo $qs;
    mysqli_query($con,$qs) or die(mysqli_error($con));
}

but '$_POST[ans1]' and '$_POST[ans2]' are always empty..can anyone help me? 但是“ $ _POST [ans1]”和“ $ _POST [ans2]”始终为空。.有人可以帮助我吗? thanks in advance! 提前致谢!

You must send ans1 and ans2 as an array to server. 您必须将ans1ans2作为数组发送到服务器。 Not as a string. 不作为字符串。

Change your while loop to this: 将您的while循环更改为此:

while($row = mysqli_fetch_array($result)) {
    echo "<input type='checkbox' id='$row[use_id]'  name='things[$row[col_id]]'  value='$row[col_id]' >$row[col]<br>";
    echo "<input placeholder='description' type='text' name='ans1[$row[col_id]]' class='$row[col_id]' id='answer1' style='display:none;'>";
    echo "<align='left'><input placeholder='source' type='text' name='ans2[$row[col_id]]' class='$row[use_id]' id='answer2'   style='display:none;'>";
}
  • Note 1: I use single quote ( ' ) in HTML attributes for better reading. 注意1:为了更好地阅读,我在HTML属性中使用了单引号( ' )。
  • Note 2: Pay attention to name='ans2[$row[col_id]]' (it's now an array) 注意2:注意name='ans2[$row[col_id]]' (现在是数组)
  • Note 3: I manually set the index of array by $row[col_id] 注意3:我通过$row[col_id]手动设置数组的索引

Now if you send this form to server, you will get such result: 现在,如果将此表单发送到服务器,您将得到以下结果:

Array
(
    [things] => Array
        (
            [1] => 1
            [4] => 4
        )

    [ans1] => Array
        (
            [1] => Description 1
            [2] => forbidden data by user
            [3] => forbidden data by user
            [4] => Description 4
            [5] => forbidden data by user
        )

    [ans2] => Array
        (
            [1] => Source 1
            [2] => forbidden data by user
            [3] => forbidden data by user
            [4] => Source 4
            [5] => forbidden data by user
        )

)

In the above array, all textbox has value as I wrote there forbidden data by user . 在上面的数组中, 所有文本框都具有值,因为我在其中forbidden data by user This is not important, because the valid data (user has checked the checkbox) is those in things array, so in the server, you must loop by that array. 这并不重要,因为有效数据(用户已选中复选框)是things数组中的数据,因此在服务器中,您必须按该数组循环。

$checkbox = $_POST['things'];

if ($checkbox) {
    foreach ($checkbox as $id => $value) {
        $ans1 = $_POST['ans1'][$id];
        $ans2 = $_POST['ans2'][$id];

        // to other stuff
    }
}

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

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