【问题标题】:Posting variable values from inside HTML forms inside PHP loop在 PHP 循环中从 HTML 表单内部发布变量值
【发布时间】:2015-05-26 05:30:33
【问题描述】:

如何从 PHP 循环内的 HTML 表单中发布变量值。

到目前为止我写的一段代码如下:

 while($row=oci_fetch_array($sql))
              {

        echo "
    <body>
        <fieldset style=\"  box-shadow: 1px 1px 10px 1px #C4C4C4; 
                            border:0; 
                            width:420px;
                            height:125px;\">
    <form action=\"buffer.php\" method=\"POST\">
    <strong>Code:</strong> <input type=\" text\" value=\" $row[0]\" name=\" code\" disabled ><br>
    <strong>Course Name:</strong> <input type=\" text\" value=\" $row[1]\" name=\" namec\" disabled><br>
    <strong>Credit:</strong> <input type=\" text\" value=\" $row[2]\" name=\" credit\" disabled ><br>
    <strong>Section:</strong> <input type=\" text\" value=\" $row[3]\" name=\" section\"disabled ><br>
    <input type=\"submit\" value=\"Add Assesment \" name=\"addasmnt\"><input type=\"submit\" value=\"Edit Attendance \" name=\"editasgmnt\">
    </br> </fieldset> </form> </body>
        ";
           $i=$i+1;          
            }

在'buffer.php'中;

<?php
session_start();
    $roll= $_SESSION['roll'];
    print_r($_SESSION);echo "<br>";
    print_r($_POST);echo "<br>";
    print_r($_GET);echo "<br>";
    print_r($_REQUEST);
?>

buffer.php的输出是

Array ( [roll] => hammad.hassan ) 
Array ( [addasmnt] => Add Assesment ) 
Array ( ) 
Array ( [addasmnt] => Add Assesment )

$_POST 没有显示任何变量。

【问题讨论】:

  • 你是说提交后$_POST是空的吗?

标签: php html arrays forms


【解决方案1】:

他们没有得到POSTed,因为输入是disabled

正如 W3C 在here 中所述:

禁用的控件无法成功。

用户代理处理表单数据的第一件事是:

第一步:确定成功的控制

但如上所述,disabled 元素不会出现在此列表中。

三个选项:

  1. 移除disabled属性
  2. 使用readonly="readonly" 而不是禁用
  3. 添加具有相同值和名称的&lt;input type="hidden"&gt;。然后将原始输入名称更改为其他名称。

隐藏输入示例

<?php
echo '
<body>
  <fieldset style="box-shadow: 1px 1px 10px 1px #C4C4C4; border:0; width:420px; height:125px;">
    <form action="buffer.php" method="POST">
      <strong>Code:</strong> <input type="text" value="'.$row[0].'" name="code_dummy" disabled><br>
      <strong>Course Name:</strong> <input type="text" value="'.$row[1].'" name="namec_dummy" disabled><br>
      <strong>Credit:</strong> <input type="text" value="'.$row[2].'" name="credit_dummy" disabled><br>
      <strong>Section:</strong> <input type="text" value="'.$row[3].'" name="section_dummy"disabled><br>
      <input type="submit" value="Add Assesment" name="addasmnt"><input type="submit" value="Edit Attendance" name="editasgmnt">
      </br>
      <input type="hidden" name="code" value="'.$row[0].'">
      <input type="hidden" name="namec" value="'.$row[1].'">
      <input type="hidden" name="credit" value="'.$row[2].'">
      <input type="hidden" name="section" value="'.$row[3].'">
    </form> 
  </fieldset> 
</body>
';

阅读更多关于disabled controls的信息。

循环中

由于您将其与循环一起使用,这将导致多个同名输入。 POST他们为arrays。在输入名称中添加[]

例如:

echo '<input type="hidden" name="code[]" value="'.$row[0].'">';

这将为您提供所有 code 输入值的数组。要为值设置键,请将其添加到 [] 中。例如[$i]

但我认为您的$i0 开头,因此在这种情况下不需要这样做,因为将从0 开始自动分配键(如果为空)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多