2015-05-26 49 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開始。