php 动态生成表单的解决办法

内容摘要
这篇文章主要为大家详细介绍了php 动态生成表单的简单示例,具有一定的参考价值,可以用来参考一下。

对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:

<?php
/**
文章正文

这篇文章主要为大家详细介绍了php 动态生成表单的简单示例,具有一定的参考价值,可以用来参考一下。

对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<code class="php">
<?php
/**
 * 动态生成表单
 *
 * @param
 * @arrange (www.idcnote.com)
 **/
define('VALID_NOT_EMPTY', '/.+/');
define('VALID_EMAIL', "/^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2,4}|museum|travel)$/i");
define('ALPHANUMERIC', '/[^a-zA-Z0-9]/');
 
function displayForm($form, $function) {
$errors = array();
 
if (!empty($_POST))
{
// Validate form and post it
foreach ($form['fields'] as $field => $options)
{
if (is_array($options) && !empty($options['rule']))
{
// Remove all non-alphanumeric characters for the id/name
$name = preg_replace(ALPHANUMERIC, null, strtolower($field));
 
if (!preg_match($options['rule'], $_POST[$name]))
{
$errors[] = $field;
}
}
}
 
if (empty($errors)) {
call_user_func($function, $_POST);
}
}
 
if (!empty($errors) || empty($_POST))
{
//Display any errors
if (!empty($errors))
{
echo '<div class="errors">';
echo 'There was an error processing your form, please check the following fields and resubmit:';
 
echo '<ul>';
foreach($errors as $field)
{
echo sprintf('<li>%s</li>', $field);
}
echo '</ul>';
echo '</div>';
}
 
// Display the form
echo '<form method="post" action="#">';
 
foreach ($form['fields'] as $field => $options)
{
// PHP will make the array key the keys index if it's not an array
$name = is_array($options) ? $field : $options;
 
// Remove all non-alphanumeric characters for the id/name
$form_name = preg_replace(ALPHANUMERIC, null, strtolower($name));
 
if ($form['escape'] == true) $name = htmlspecialchars($name);
 
echo sprintf('<label for="%s">%s: </label>', $form_name, $name);
 
// Default is a standard text input
if (!is_array($options) || !isset($options['type']) || $options['type'] == 'text')
{
echo sprintf('<input type="text" id="%s" name="%s" value="%s" />', $form_name, $form_name, $_POST[$form_name]);
} elseif ($options['type'] == 'textarea') {
echo sprintf('<textarea id="%s" name="%s" cols="%s" rows="%s">%s</textarea>', $form_name, $form_name, $options['cols'], $options['rows'], $_POST[$form_name]);
} elseif ($options['type'] == 'select') {
echo sprintf('<select id="%s" name="%s">', $form_name, $form_name);
foreach ($options['items'] as $item)
{
if ($form['escape'] == true) $item = htmlspecialchars($item);
 
echo sprintf('<option value="%s">%s</option>', $item, $item);
}
echo '</select>';
}
echo '<br />'. "\n";
}
 
echo '<input type="submit" value="Send" />';
echo '</form>';
}
}
?></code>

注:关于php 动态生成表单的简单示例的内容就先介绍到这里,更多相关文章的可以留意

代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!