我正在为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>
答案 0 :(得分:1)
我通过设置增量javascript global var:
来实现这样的目标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
是处理动态表单提交事件中数据的位置。
如果您需要更多说明,请与我们联系。