简体   繁体   English

动态添加输入字段的PHP表单处理

[英]PHP form handling for dynamically added input fields

I am having trouble with the PHP form handling for dynamically added input fields. 我在处理动态添加的输入字段的PHP表单时遇到麻烦。

The JavaScript and HTML code I am testing with can be found at http://jsfiddle.net/wd5y9/ . 我正在测试的JavaScript和HTML代码可以在http://jsfiddle.net/wd5y9/中找到。 This part, obviously, works like a charm. 显然,这部分工作就像一个魅力。

Here is my PHP code - 这是我的PHP代码-

$emailSubject = 'Test Form';
$mailto = 'xxx@xxx.com';

$project = $_POST['project'];
$department = $_POST['department'];
$task = $_POST['task'];
$hours = $_POST['hours'];
$comment = $_POST['comment'];

date_default_timezone_set('America/Chicago');
$date = date('l F j, Y g:ia T');

$body = <<<EOD

<h2>Form Information</h2>
<table>
<tr>
<td><strong>Project</strong>: $project<br /></td>
<td><strong>Department</strong>: $department<br /></td>
<td><strong>Task</strong>: $task<br /></td>
<td><strong>Hours</strong>: $hours<br /></td>
<td><strong>Comment</strong>: $comment<br /></td>
</tr>
</table>

<p>Form submitted $date.</p>

EOD;

$headers = "From: xxx@xxx.com\r\n"; 
$headers .= "Content-type: text/html\r\n"; 
$success = mail($mailto, $emailSubject, $body, $headers); 

Here is how the e-mail came through after testing - 经过测试,这是电子邮件的发送方式-

Form Information Project: Array Department: Array Task: Array Hours: Array Comment: Array 表单信息项目:阵列部门:阵列任务:阵列工作时间:阵列注释:阵列

So what am I doing wrong? 那我在做什么错? Thanks! 谢谢!

You probably have arrays posted. 您可能发布了数组。 So you need to do something like 所以你需要做类似的事情

$project = $_POST['project']['somethingHere'];

If you post a print_r of your $_POST ( print_r($_POST) ) we will be able to help you even more. 如果您发布$ _POST( print_r($_POST) )的print_r($_POST)我们将为您提供更多帮助。

EDIT 编辑

From your comment I see that you have two projects, departments, comments etc. 从您的评论中,我看到您有两个项目,部门,评论等。

So in order for your code to work it should be a loop. 因此,为了使您的代码起作用,它应该是一个循环。

for($i=0; $i<=count($_POST['project']); $i++)
{
 <h2>Form Information</h2>
<table>
<tr>
<td><strong>Project</strong>: $project[$i]<br /></td>
<td><strong>Department</strong>: $department[$i]<br /></td>
<td><strong>Task</strong>: $task[$i]<br /></td>
<td><strong>Hours</strong>: $hours[$i]<br /></td>
<td><strong>Comment</strong>: $comment[$i]<br /></td>
</tr>
</table>
}

You have multiple rows (assuming they generate them) posting to your server with the same name, so the values are actually arrays of those values. 您有多个行(假定它们生成它们)以相同的名称发布到您的服务器,因此这些值实际上是这些值的数组。

So to access the first project, you'd just access the first value of the $project array: 因此,要访问第一个项目,您只需访问$ project数组的第一个值:

$project = $_POST['project'];
$firstProject = $project[0];

Or better yet, just loop over them all to spit out all rows in your email, something like this: 或者更好的方法是,将它们全部循环以吐出电子邮件中的所有行,如下所示:

foreach ($project as $i => $thisProject) {
?>

<h2>Form Information</h2>
<table>
<tr>
<td><strong>Project</strong>: $project[$i]<br /></td>
<td><strong>Department</strong>: $department[$i]<br /></td>
<td><strong>Task</strong>: $task[$i]<br /></td>
<td><strong>Hours</strong>: $hours[$i]<br /></td>
<td><strong>Comment</strong>: $comment[$i]<br /></td>
</tr>
</table>

<?php }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM