【问题标题】:How can I create a php mail handler for a dynamically created form如何为动态创建的表单创建 php 邮件处理程序
【发布时间】:2016-06-05 19:49:45
【问题描述】:

我正在为 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>

【问题讨论】:

  • 可以给表单元素添加一个静态的id,然后用ajax/js处理吗?
  • @OliverQueen 并非如此,因为用户可能会向页面添加多个表单
  • 您是否可以控制表单中的 HTML 内容?
  • @OliverQueen 我认为是的 - 我基本上是从头开始编写这个插件,所以现在可以改变大部分内容
  • 好的,我有个主意,我要在回答中写下我的建议

标签: php wordpress forms


【解决方案1】:

我会通过设置增量 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 是处理来自动态表单提交事件的数据的位置。

如果您需要更多说明,请告诉我。

【讨论】:

  • 谢谢你,我认为它会为我指明正确的方向。我面临的问题是用户可以创建完全独特的表单(包括所有输入的名称)。例如,用户 A 可以创建 ... 而用户 B 可以创建 所以显然我的这里的问题是尝试捕获每个表单的唯一性,并通过我的处理程序对其进行解析 - 不确定您的脚本是否允许我这样做?
  • 如果有帮助,让我快速添加我的表单构建器 php 代码 - 虽然不知道它在上下文之外会有多大意义
  • 我建议在某个地方设置一个空白 div,您可以在其中填充包含名称列表(用逗号分隔)的其他 div,然后您可以使用表单标识符来获取特定的 div该表单的名称,然后用 split() javascript 函数分隔它们
  • 其实这不是一个坏主意,你认为我可以把它作为一个隐藏的输入而侥幸吗
  • 是的,我就是这么做的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-23
  • 2016-08-20
  • 1970-01-01
  • 2020-04-15
  • 2019-04-13
  • 1970-01-01
相关资源
最近更新 更多