【发布时间】: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 我认为是的 - 我基本上是从头开始编写这个插件,所以现在可以改变大部分内容
-
好的,我有个主意,我要在回答中写下我的建议