如何为动态创建的表单创建 php 邮件处理程序


How can I create a php mail handler for a dynamically created form

我正在为wordpress创建一个表单构建器插件,允许用户构建自己的自定义表单。

我已经设法设计了表单构建器,现在我正在查看表单提交处理程序。我正在使用一个php处理程序,类似于这样:

<?php
// process.php
$errors         = array();      // array to hold validation errors
$data           = array();      // array to pass back data
// validate the variables ======================================================
// if any of these variables don't exist, add an error to our $errors array
if (empty($_POST['name']))
    $errors['name'] = 'Name is required.';
if (empty($_POST['email']))
    $errors['email'] = 'Email is required.';
if (empty($_POST['superheroAlias']))
    $errors['superheroAlias'] = 'Superhero alias is required.';
// return a response ===========================================================
// if there are any errors in our errors array, return a success boolean of false
if ( ! empty($errors)) {
    // if there are items in our errors array, return those errors
    $data['success'] = false;
    $data['errors']  = $errors;
} else {
    // if there are no errors process our form, then return a message
    // DO ALL YOUR FORM PROCESSING HERE
    // THIS CAN BE WHATEVER YOU WANT TO DO (LOGIN, SAVE, UPDATE, WHATEVER)
    // show a message of success and provide a true success variable
    $data['success'] = true;
    $data['message'] = 'Success!';
}
// return all our data to an AJAX call
echo json_encode($data);

但是,如前所述,我的表单是动态创建的,因此我无法使用硬编码变量。

我不确定解决这个问题的好方法,任何人都可以建议一种方法,我可以将主表单构建器文件中动态创建的变量用作此处理程序文件的一部分?

这是我的 php 生成器代码:

<form action="./includes/process.php" method="POST">
                    <?php foreach ( wp_parse_id_list( $widget[ 'form_builder_ids' ] ) as $form_builder_key ) {
....
<div class="media-body <?php echo ( isset( $item['design']['fonts'][ 'align' ] ) ) ? $item['design']['fonts'][ 'align' ] : ''; ?>">
                                            <?php if( $this->check_and_return( $item, 'label') ) { ?>
                                                <label>
                                                    <?php echo $item['label']; ?>
                                                        <?php if( $this->check_and_return( $item, 'required') ) { ?>
                                                            <span class="required" style="color:#c0392b;">*</span>
                                                        <?php } ?>
                                                </label>
                                            <?php } ?>
                                            <?php if( $this->check_and_return( $item, 'input_type') ) { ?>
                                            <?php
                                                $input_type_array = array('select', 'textarea', 'checkbox', 'radio');
                                                if( !in_array( $item['input_type'] ,$input_type_array ) ) {?>
                                                    <input type="<?php echo $item['input_type']; ?>" name="<?php echo $item['input_name']; ?>" <?php if( $this->check_and_return( $item, 'required') ) { echo 'required'; } ?>>
                                                <?php } else if ($item['input_type'] == 'textarea') { ?>
                                                    <textarea name="<?php echo $item['input_name']; ?>" <?php if( $this->check_and_return( $item, 'required') ) { echo 'required'; } ?>></textarea>
                                                <?php } else if ($item['input_type'] == 'select') { ?>
                                                    <select name="<?php echo $item['input_name']; ?>" <?php if( $this->check_and_return( $item, 'required') ) { echo 'required'; } ?>>
                                                        <?php foreach(explode("'n", $item['select_options']) as $select_option) { ?>
                                                            <option value="<?php echo preg_replace('/'s+/','', $select_option); ?>"><?php echo $select_option; ?></option>
                                                        <?php } ?>
                                                    </select>
                                                <?php } else if ($item['input_type'] == 'checkbox') { ?>
                                                        <?php foreach(explode("'n", $item['select_options']) as $select_option) { ?>
                                                            <input type="checkbox" name="<?php echo $item['input_name']; ?>" value="<?php echo preg_replace('/'s+/','', $select_option); ?>" <?php if( $this->check_and_return( $item, 'required') ) { echo 'required'; } ?>><?php echo $select_option; ?>
                                                        <?php } ?>

                                                <?php } else if ($item['input_type'] == 'radio') { ?>
                                                        <?php foreach(explode("'n", $item['select_options']) as $select_option) { ?>
                                                            <input type="radio" name="<?php echo $item['input_name']; ?>" value="<?php echo preg_replace('/'s+/','', $select_option); ?>" <?php if( $this->check_and_return( $item, 'required') ) { echo 'required'; } ?>><?php echo $select_option; ?>
                                                        <?php } ?>

                                                <?php } ?>
                                            <?php } ?>
                                    </div>
....
</form>

我会通过设置一个增量的javascript全局变量来实现这样的目标:

var x = 0;

function addForm(){
    var formHTML = '<form onsubmit = "submitForm(' + x + '); return false;"> <input id = "name' + x + '" ...  </form>';
    x = x+1;
}
function submitForm(formIdentifier){
    var name = document.getElementById("name"+formIdentifier).value; //how you can get the form values from dynamic forms
    //process form with ajax from here
}

其中addForm()是将表单添加到页面的代码,submitForm是处理来自动态表单提交事件的数据的位置。

如果您需要更多澄清,请告诉我。