无法在相应复选框的帮助下获取文本字段的值.请帮忙


unable to get the value of the text field with the help of respective checkboxes. please help

我有点怀疑。我有3个文本框,每个文本框旁边都有复选框。我想显示仅单击相应复选框的那些文本框的值。以下是所附的HTML和PHP代码:

<html>
    <head>
    </head>
        <body>
            <form name="f" method="post" action="4.php">
                <table>
                  <tr>
                    <th> Facility </th> 
                  </tr>
                  <tr>
                    <td><input type="text" name="a1" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Hostel"></td>
                  </tr> 
                  <tr>  
                    <td><input type="text" name="b1" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Transport"></td>
                  </tr> 
                  <tr>
                    <td><input type="text" name="c1" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Food"></td>
                  </tr> 
                  <tr>
                    <td colspan="3"><input type="submit" value="submit" /></td>
                  </tr>
                 </table>   
               </form>
          </body>
</html>

下面是PHP部分。

<?php
    $a=$_POST['a1'];
    $b=$_POST['b1'];
    $c=$_POST['c1'];
    $facilityArray = $_POST['facility'];
    $facility = "";
    if(count($facilityArray) > 0)
    {
        foreach($facilityArray as $fac)
        {
            $facility .= " " . $fac;
        }
    }
    echo $facility; echo "<br>"; 
    echo $a; echo "<br>";
    echo $b; echo "<br>";
    echo $c;
?>

在以下代码的帮助下,我能够显示选中复选框的所有值。我还可以显示所有文本框的值。但实际上,我只想显示那些单击了相应复选框的文本框的值。我知道这可能是一个非常基本的问题,但请帮助我在PHP中成长。提前感谢…:(

您的文本框也应该在数组post中才能实现这一点。

为了实现这一点,将输入行更改为:

<td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>

从php中,您将获得数组中发布的文本框,如下所示:

$textbox=$_POST['textboxes'];

然后你应该循环通过复选框数组,如果对应的复选框是"0";关于";(单击),然后显示文本框值。要做到这一点,你还需要一个计数器来确保你在复选框和文本框的同一数组索引上:

 if(count($facilityArray) > 0)
{
    $i = 0;
    foreach($facilityArray as $fac)
    {
        if($fac == "on")
        {
            echo $textbox[$i] . "</br>";
        }
        $i ++;
    }
}

我还为您的提交按钮添加了一个名称,这样您只会在提交表单时检查表单。你的页面现在应该是这样的:

        <?php
    if(isset($_POST['submit']))
    {
        $textbox=$_POST['textboxes'];
    
        $facilityArray = $_POST['facility'];
        
        if(count($facilityArray) > 0)
        {
            $i = 0;
            foreach($facilityArray as $fac)
            {
                if($fac == "on")
                {
                    echo $textbox[$i] . "</br>";
                }
                $i ++;
            }
        }
    
    }
    ?>
    <form name="f" method="post" action="4.php">
                
    
        <table>
                      <tr>
                        <th> Facility </th> 
                      </tr>
                      <tr>
                        <td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
                      </tr> 
                      <tr>  
                        <td><input type="text" name="textboxes[]" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
                      </tr> 
                      <tr>
                        <td><input type="text" name="textboxes[]" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
                      </tr> 
                      <tr>
                        <td colspan="3"><input name="submit" type="submit" value="submit" /></td>
                      </tr>
        </table>
    </form>

更新:

为了确保$_POST变量在分配给变量之前存在,我们使用isset()。在您的情况下,只需将php段更新为:

<?php
    if(isset($_POST['submit']))
    {
        if(isset($_POST['textboxes']))
        {
            $textbox=$_POST['textboxes'];
            
            if(isset($_POST['facility']))
            {
                $facilityArray = $_POST['facility'];
                
                if(count($facilityArray) > 0)
                {
                    $i = 0;
                    foreach($facilityArray as $fac)
                    {
                        if($fac == "on")
                        {
                            echo $textbox[$i] . "</br>";
                        }
                        $i ++;
                    }
                }
            }
        }
    
    }
?>

其中唯一的更改是添加另外两个if语句,根据$_POST变量是否已成功发布从isset()函数中获取布尔标志

if(isset($_POST['textboxes']))

if(isset($_POST['facility']))