调试PHP表单处理,与动态HTML字段


Debugging PHP Form Handling, with Dynamic HTML Fields

我正在使用这个HTML代码来允许用户添加更多字段并从这个订单中删除它们。

<!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) {
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	if(rowCount < 20){							// limit the user from creating fields more than your limits
		var row = table.insertRow(rowCount);
		var colCount = table.rows[0].cells.length;
		for(var i=0; i<colCount; i++) {
			var newcell = row.insertCell(i);
			newcell.innerHTML = table.rows[0].cells[i].innerHTML;
		}
	}else{
		 alert("Maximum Photos per order is 20.");
			   
	}
}
function deleteRow(tableID) {
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	for(var i=0; i<rowCount; i++) {
		var row = table.rows[i];
		var chkbox = row.cells[0].childNodes[0];
		if(null != chkbox && true == chkbox.checked) {
			if(rowCount <= 1) { 						// limit the user from removing all the fields
				alert("Cannot Remove all Picture fields.");
				break;
			}
			table.deleteRow(i);
			rowCount--;
			i--;
		}
	}
}
</script> 
</head>
<body>
	<form action="process.php" class="register" method="POST">
		<fieldset class="row1">
				<legend>Order Details</legend>
				<p> 
					<input type="button" value="Add Picture" onClick="addRow('dataTable')" /> 
					<input type="button" value="Remove Picture" onClick="deleteRow('dataTable')"  /> 
					<p>(All acions apply only to entries with check marked check boxes only.)</p>
				</p>
               <table id="dataTable" class="form" border="1"> <!-- These Fields get duplicated -->
                  <tbody>
                    <tr>
                      <p>
						<td><input type="checkbox" required="required" name="chk[]" checked="" /></td>
						 <td>
							<label for="BX_file">Filename</label>
							<input type="text" required="required" class="small"  name="BX_file[]">
					     </td>
						 <td>
							<label for="BX_gallery">Gallery</label>
							<select id="BX_gallery" name="BX_gallery" required="required">
								<option>....</option>
								<option>Nature</option>
								<option>Stilllife</option>
								<option>Portrait</option>
								<option>Constructed</option>
								<option>Nature</option>
								<option>Black & White </option>
								<option>Rotarian Dinner Dance 25/04/2015</option>
							</select>
						 </td>
						 <td>
							<label for="BX_medium">Medium</label>
							<select id="BX_medium" name="BX_medium" required="required">
								<option>....</option>
								<option>Small Print</option>
								<option>Medium Print</option>
								<option>Large Print</option>
								<option>Custom Print</option>
								<option>CD Image Disc</option>
								<option>Digital Download</option>
							</select>
						 </td>
						 <td>
							<label for="BX_quantity">Quantity</label>
							<input type="number" required="required" class="small"  name="BX_quantity[]">
					     </td>
							</p>
                    </tr>
                    </tbody>
                </table> <!-- End of Fields that are duplicated. -->
				<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>				
				<div class="clear"></div>
            </fieldset>
			<fieldset class="row3">
                <legend>Other</legend>
                <p class="notes">
                    <input type="text">
                    <label>Additional Notes</label>
                </p>				
				<div class="clear"></div>
            </fieldset>
			<input class="submit" type="submit" value="Confirm &raquo;" />
				<div class="clear"></div>
        </fieldset>
	</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("harrysanderson@live.co.uk","Order Test",$str_ORD)
//Sends email.

这是我一直在遵循的教程:

任何帮助都非常感谢!不好意思,我是PHP的新手

post数组中的键值区分大小写。在POST中使用与输入元素相同的名称

例如,

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

此处POST应为$_POST['BX_file']

你的代码有一些问题:

  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 )