2015-08-18 87 views
0

我使用這個HTML代碼來允許用戶添加更多字段並將其從此訂單中刪除。使用動態HTML字段調試PHP表格處理

<!DOCTYPE HTML> 
 
<html> 
 
<head> 
 
<title>Dynamic Form Processing with PHP | Tech Stream</title> 
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
 
<link rel="stylesheet" type="text/css" href="css/default.css"/> 
 
<script type="text/javascript"> 
 
function addRow(tableID) { 
 
\t var table = document.getElementById(tableID); 
 
\t var rowCount = table.rows.length; 
 
\t if(rowCount < 20){ \t \t \t \t \t \t \t // limit the user from creating fields more than your limits 
 
\t \t var row = table.insertRow(rowCount); 
 
\t \t var colCount = table.rows[0].cells.length; 
 
\t \t for(var i=0; i<colCount; i++) { 
 
\t \t \t var newcell = row.insertCell(i); 
 
\t \t \t newcell.innerHTML = table.rows[0].cells[i].innerHTML; 
 
\t \t } 
 
\t }else{ 
 
\t \t alert("Maximum Photos per order is 20."); 
 
\t \t \t  
 
\t } 
 
} 
 

 
function deleteRow(tableID) { 
 
\t var table = document.getElementById(tableID); 
 
\t var rowCount = table.rows.length; 
 
\t for(var i=0; i<rowCount; i++) { 
 
\t \t var row = table.rows[i]; 
 
\t \t var chkbox = row.cells[0].childNodes[0]; 
 
\t \t if(null != chkbox && true == chkbox.checked) { 
 
\t \t \t if(rowCount <= 1) { \t \t \t \t \t \t // limit the user from removing all the fields 
 
\t \t \t \t alert("Cannot Remove all Picture fields."); 
 
\t \t \t \t break; 
 
\t \t \t } 
 
\t \t \t table.deleteRow(i); 
 
\t \t \t rowCount--; 
 
\t \t \t i--; 
 
\t \t } 
 
\t } 
 
} 
 
</script> 
 
</head> 
 
<body> 
 
\t <form action="process.php" class="register" method="POST"> 
 
\t \t <fieldset class="row1"> 
 
\t \t \t \t <legend>Order Details</legend> 
 
\t \t \t \t <p> 
 
\t \t \t \t \t <input type="button" value="Add Picture" onClick="addRow('dataTable')" /> 
 
\t \t \t \t \t <input type="button" value="Remove Picture" onClick="deleteRow('dataTable')" /> 
 
\t \t \t \t \t <p>(All acions apply only to entries with check marked check boxes only.)</p> 
 
\t \t \t \t </p> 
 
       <table id="dataTable" class="form" border="1"> <!-- These Fields get duplicated --> 
 
        <tbody> 
 
        <tr> 
 
         <p> 
 
\t \t \t \t \t \t <td><input type="checkbox" required="required" name="chk[]" checked="" /></td> 
 
\t \t \t \t \t \t <td> 
 
\t \t \t \t \t \t \t <label for="BX_file">Filename</label> 
 
\t \t \t \t \t \t \t <input type="text" required="required" class="small" name="BX_file[]"> 
 
\t \t \t \t \t  </td> 
 
\t \t \t \t \t \t <td> 
 
\t \t \t \t \t \t \t <label for="BX_gallery">Gallery</label> 
 
\t \t \t \t \t \t \t <select id="BX_gallery" name="BX_gallery" required="required"> 
 
\t \t \t \t \t \t \t \t <option>....</option> 
 
\t \t \t \t \t \t \t \t <option>Nature</option> 
 
\t \t \t \t \t \t \t \t <option>Stilllife</option> 
 
\t \t \t \t \t \t \t \t <option>Portrait</option> 
 
\t \t \t \t \t \t \t \t <option>Constructed</option> 
 
\t \t \t \t \t \t \t \t <option>Nature</option> 
 
\t \t \t \t \t \t \t \t <option>Black & White </option> 
 
\t \t \t \t \t \t \t \t <option>Rotarian Dinner Dance 25/04/2015</option> 
 
\t \t \t \t \t \t \t </select> 
 
\t \t \t \t \t \t </td> 
 
\t \t \t \t \t \t <td> 
 
\t \t \t \t \t \t \t <label for="BX_medium">Medium</label> 
 
\t \t \t \t \t \t \t <select id="BX_medium" name="BX_medium" required="required"> 
 
\t \t \t \t \t \t \t \t <option>....</option> 
 
\t \t \t \t \t \t \t \t <option>Small Print</option> 
 
\t \t \t \t \t \t \t \t <option>Medium Print</option> 
 
\t \t \t \t \t \t \t \t <option>Large Print</option> 
 
\t \t \t \t \t \t \t \t <option>Custom Print</option> 
 
\t \t \t \t \t \t \t \t <option>CD Image Disc</option> 
 
\t \t \t \t \t \t \t \t <option>Digital Download</option> 
 
\t \t \t \t \t \t \t </select> 
 
\t \t \t \t \t \t </td> 
 
\t \t \t \t \t \t <td> 
 
\t \t \t \t \t \t \t <label for="BX_quantity">Quantity</label> 
 
\t \t \t \t \t \t \t <input type="number" required="required" class="small" name="BX_quantity[]"> 
 
\t \t \t \t \t  </td> 
 
\t \t \t \t \t \t \t </p> 
 
        </tr> 
 
        </tbody> 
 
       </table> <!-- End of Fields that are duplicated. --> 
 
\t \t \t \t <fieldset class="row2"> 
 
       <legend>Terms and Mailing</legend> 
 
       <p class="agreement"> 
 
        <input type="checkbox" value=""/> 
 
        <label>* I accept the <a href="#">Terms and Conditions</a></label> 
 
       </p> \t \t \t \t 
 
\t \t \t \t <div class="clear"></div> 
 
      </fieldset> 
 
\t \t \t <fieldset class="row3"> 
 
       <legend>Other</legend> 
 
       <p class="notes"> 
 
        <input type="text"> 
 
        <label>Additional Notes</label> 
 
       </p> \t \t \t \t 
 
\t \t \t \t <div class="clear"></div> 
 
      </fieldset> 
 
\t \t \t <input class="submit" type="submit" value="Confirm &raquo;" /> 
 
\t \t \t \t <div class="clear"></div> 
 
     </fieldset> 
 
\t </form> 
 
</body> 
 
</html>

我現在嘗試通過PHP,使他們可以通過電子郵件發送給我來處理場投入。然而,我似乎無法得到它的工作。任何人都可以指出我的PHP代碼出錯了嗎?

<?php 
$str_File = $_POST['BX_FILE']; 
$str_Gall = $_POST['BX_GALLERY']; 
$str_Med = $_POST['BX_MEDIUM']; 
$int_Quant = $_POST['BX_QUANT']; 
$str_Notes = $_POST['NOTES']; 
//Gets all the values and saves them to the variables in array format. 

$int_Num = count($FILE) //Gets the size of th array. 

for ($x = 1; $x <= $int_Num; $x++) { 
$str_ORD = "Order Field: $x, Filename: $str_File[$x], Gallery: $str_Gall[$x], Photo Medium: $str_Med[$x], Quantity: $int_Quant[$x]" 
} 
//Loops through all the arrays assembling the message for the email. 

mail("[email protected]","Order Test",$str_ORD) 
//Sends email. 

This是教程中,我一直在關注:

任何幫助,不勝感激! 對不起,我是一個初學者,當涉及到PHP

+0

是什麼 「似乎無法得到它的工作」 是什麼意思?它如何失敗? – David

+0

如果用'$ FILE'表示**超全局變量**,則表示'$ _FILES' –

+0

由於未發送電子郵件而導致頁面更改失敗,因此您可以看到PHP代碼。抱歉,$ str_File僅用於保存HTML表單中的值。 –

回答

0

post數組中的鍵值區分大小寫。在POST中使用您在輸入元素中使用的相同名稱。

例如, <input type="text" required="required" class="small" name="BX_file[]">

這裏,POST應$_POST['BX_file']

+0

我試過更正名稱,以便它們正確匹配,但我仍然沒有運氣? –

0

有一些問題與您的代碼:

  1. 你的字段名稱是相同的。 BX_file []不會變成一個數組(如果是這樣的話),只是因爲你在它的末尾加了括號。您需要通過JS動態管理輸入名稱,否則您的字段將在POST事件中相互覆蓋。

見例如:

<!DOCTYPE html> 
<html> 
    <head></head> 
     <body> 
     <form method="post" action="test5.php"> 
      <input type="text" name="input_foo"> 
      <input type="text" name="input_bar"> 
      <input type="text" name="input_foo"> 
      <input type="submit" value="post"> 
     </form> 
    </body> 
</html> 

Actioin文件:

<?php 

print_r($_POST); 

輸出:

Array ([input_foo] => test3 [input_bar] => test2)