• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

php 如何动态生成表单代码

PHP 水墨上仙 2575次浏览

php动态生成表单代码

<?php
 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>';
 }
 }
?>

 


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明php 如何动态生成表单代码
喜欢 (0)
加载中……