【问题标题】:Debugging PHP Form Handling, with Dynamic HTML Fields使用动态 HTML 字段调试 PHP 表单处理
【发布时间】:2015-08-18 17:21:18
【问题描述】:

我正在使用这个 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.

This是我一直关注的教程:

非常感谢任何帮助! 抱歉,我是 PHP 的初学者。

【问题讨论】:

  • “似乎无法正常工作”是什么意思?它是如何失败的?
  • 如果$FILE 是指超全局变量,它就是$_FILES
  • 它失败了,因为没有发送电子邮件,而是页面更改,以便您可以看到 PHP 代码。抱歉,$str_File 仅用于保存 HTML 表单中的值。

标签: javascript php jquery html debugging


【解决方案1】:

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

例如, &lt;input type="text" required="required" class="small" name="BX_file[]"&gt;

这里,POST 应该是$_POST['BX_file']

【讨论】:

  • 我已尝试更正名称以使它们正确匹配,但我仍然没有运气?
【解决方案2】:

您的代码存在一些问题:

  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>

动作文件:

<?php

print_r($_POST);

输出:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 2014-08-08
    • 1970-01-01
    • 2015-08-04
    • 2016-08-16
    • 1970-01-01
    相关资源
    最近更新 更多