使用 PHP 从动态输入字段中捕获数组值并通过电子邮件发送它们
Posted
技术标签:
【中文标题】使用 PHP 从动态输入字段中捕获数组值并通过电子邮件发送它们【英文标题】:Capture Array Values from Dynamic input Fields using PHP and email them 【发布时间】:2015-10-02 13:50:56 【问题描述】:我有一个非常大的表单,在这个表单中有多个动态表单字段可以由用户添加。我需要使用 php 和电子邮件来捕获这些,不需要 mysqli 连接。我使用一些 javascript 来处理出现和消失的动态表单字段。
其中一个 html 动态表单字段如下所示--
<div class="col-sm-1">
<select id="about_you_dep-gender[]" name="about_you_dep- gender[]" class="form-control">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select><div class="help">Gender</div>
</div>
php 当前看起来像这样 - 如果我现在提交表单,该字段的内容只是说:array
<?php
if(!isset($_POST['submit']))
echo "error; you need to submit the form!";
$about_you_dep_gender = $_POST['about_you_dep-gender'];
$email_subject = "New Form submission";
$email_body = "You have received a new filled out form from client $about_you_name.\n\n".
"Dependant Gender: $about_you_dep_gender \n".
javascript 在下面,这部分表单似乎工作正常。
<script>
$(document).ready(function()
// The maximum number of options
var MAX_OPTIONS = 3;
$('#custom')
.formValidation(
framework: 'bootstrap',
icon:
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
)
// Add button click handler
.on('click', '.addButton', function()
var $template = $('#depTemplate'),
$clone = $template
.clone()
.removeClass('hide')
.removeAttr('id')
.insertBefore($template),
$dep_name = $clone.find('[name="about_you_dep-name[]"]')
$dep_gender = $clone.find('[name="about_you_dep-gender[]"]')
$dep_dob = $clone.find('[name="about_you_dep-dob[]"]')
$dep_fin = $clone.find('[name="about_you_dep-fin[]"]')
$dep_until = $clone.find('[name="about_you_dep-until[]"]');
// Add new field
$('#custom')
.formValidation('addField', $dep_name)
.formValidation('addField', $dep_gender)
.formValidation('addField', $dep_dob)
.formValidation('addField', $dep_fin)
.formValidation('addField', $dep_until);
)
// Remove button click handler
.on('click', '.removeButton', function()
var $row = $(this).parents('.form-group'),
$dep_name = $row.find('[name="about_you_dep-name[]"]')
$dep_gender = $row.find('[name="about_you_dep-gender[]"]')
$dep_dob = $row.find('[name="about_you_dep-dob[]"]')
$dep_fin = $row.find('[name="about_you_dep-fin[]"]')
$dep_until = $row.find('[name="about_you_dep-until[]"]');
// Remove element containing the option
$row.remove();
// Remove field
$('#custom')
.formValidation('removeField', $dep_name)
.formValidation('removeField', $dep_gender)
.formValidation('removeField', $dep_dob)
.formValidation('removeField', $dep_fin)
.formValidation('removeField', $dep_until);
)
// Called after adding new field
.on('added.field.fv', function(e, data)
// data.field --> The field name
// data.element --> The new field element
// data.options --> The new field options
if (data.field === 'about_you_dep-name[]')
if ($('#custom').find(':visible[name="about_you_dep-name[]"]').length >= MAX_OPTIONS)
$('#custom').find('.addButton').attr('disabled', 'disabled');
)
// Called after removing the field
.on('removed.field.fv', function(e, data)
if (data.field === 'about_you_dep-name[]')
if ($('#custom').find(':visible[name="about_you_dep-name[]"]').length < MAX_OPTIONS)
$('#custom').find('.addButton').removeAttr('disabled');
);
);
目前,当我处理表单时,它会返回答案:数组,而不是表单字段的实际输入。任何帮助将不胜感激。提前非常感谢你:)
【问题讨论】:
【参考方案1】:你必须遍历数组
<?php
if(!isset($_POST['submit']))
$dependents = array();
foreach ($_POST['about_you_dep-gender'] as $key => $value)
$dependents[] = array(
'name' => $_POST['about_you_dep-name'][$key],
'gender' => $_POST['about_you_dep-gender'][$key],
'dob' => $_POST['about_you_dep-dob'][$key],
);
$email_subject = "New Form submission";
$email_body = "You have received a new filled out form from client.\n\n";
$email_body .= "Name: " . $aboutYouName . "\n";
foreach ($dependents as $dependent)
$email_body .= "Dependent Name: " . $dependent['name'] . "\n";
$email_body .= "Dependent Gender: " . $dependent['gender'] . "\n";
$email_body .= "Dependent DOB: " . $dependent['dob'] . "\n";
// Send Email
【讨论】:
以上是关于使用 PHP 从动态输入字段中捕获数组值并通过电子邮件发送它们的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 jQuery+Codeigniter 获取值并显示在输入字段中?