2015-05-26 45 views
0

如何发布PHP循环内的HTML表单中的变量值?从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的未示出的任何变量。

+0

你是说你提交后'$ _POST'是空的吗? – Rasclatt

回答

2

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

如W3C国here

残疾人控件不能成功。

第一件事什么用户代理确实在处理形式的数据是:

步骤一:识别成功的控制

但正如上面所说的,disabled元件不会是在此名单。

三个选项:

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

隐藏输入例如

<?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他们为array s。将[]添加到输入名称。

例如:

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

这会给你所有code输入值的数组。要为值设置一个键,请将其添加到[]之内。例如[$i]

。不过我想你$i开始与0,所以这是没有必要在这种情况下,密钥将被自动分配(如果为空)从0开始。