使用PHP从动态输入字段捕获数组值并通过电子邮件发送它们

时间:2015-07-14 04:55:33

标签: javascript php arrays forms dynamic

我有一个非常大的表单,在这种形式中有多个动态表单字段,可以由用户添加。我需要使用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');
            }
        }
    });

});  

目前,当我处理表单时,它返回的是:answer:数组,而不是表单字段中的实际输入。任何帮助将不胜感激。非常感谢你提前:))

1 个答案:

答案 0 :(得分:0)

你必须循环遍历数组

<?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
}