WordPress教程

WordPress 怎么制作一个可留言的单网页

阿里云

网上有很多的调查类单网页,可以用于用户在线填写提交。然后管理员可以在后台查看到用户的留言信息。效果如下图:

下面就介绍一下怎么制作这种可留言的单页面。

第一步

制作前台用户填写表单页面 form.php,使用 Form 标签。

也想出现在这里?联系我们
创客主机
  1. <form class="ludou-tougao fbform" method="post" action="<?php echo $_SERVER['REQUEST_URI']; $current_user = wp_get_current_user(); ?>">
  2. <div class="tgform">
  3. <p><input id="author" type="text" size="40″ value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_login; ?>" name="tougao_authorname" /><label>昵称(*必填)</label></p>
  4. <p><input id="email" type="text" size="40″ value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_email; ?>" name="tougao_authoremail" /><label>邮箱(*必填)</label></p>
  5. <p><input id="tougao_title" type="text" size="40″ value="" name="tougao_title" /><label>文章标题(*必填)</label></p>
  6. <p><?php wp_dropdown_categories('show_option_none=请选择文章分类&show_count=1&hierarchical=1&hide_empty=0'); ?><label>文章分类(*必填)</label></p>
  7. <p><textarea rows="15″ cols="55″ id="tougao" name="tougao_content"></textarea></p>
  8. <p class="tjbtn">
  9. <input type="hidden" value="send" name="tougao_form" />
  10. <input id="submit" type="submit" value="提交" />
  11. <input id="reset" type="reset" value="重填" />
  12. </p>
  13. </div>
  14. </form>

第二步

制作表单数据验证,用于验证用户提交的数据。防止别人填写垃圾数据。

  1. <?php
  2. if( isset($_POST['tougao_form']) && $_POST['tougao_form'] == 'send') {
  3. global $wpdb;
  4. $current_url = '/'; //用户留言页面
  5. $last_post = $wpdb->get_var("SELECT 'post_date' FROM '$wpdb->posts' ORDER BY 'post_date' DESC LIMIT 1");// 表单变量初始化
  6. $name = isset( $_POST['tougao_authorname'] ) ? trim(HTMLspecialchars($_POST['tougao_authorname'], ENT_QUOTES)) : '';
  7. $email = isset( $_POST['tougao_authoremail'] ) ? trim(htmlspecialchars($_POST['tougao_authoremail'], ENT_QUOTES)) : '';
  8. $tell = isset( $_POST['tougao_tell'] ) ? trim(htmlspecialchars($_POST['tougao_tell'], ENT_QUOTES)) : '';
  9. $dizhi = isset( $_POST['tougao_dizhi'] ) ? trim(htmlspecialchars($_POST['tougao_dizhi'], ENT_QUOTES)) : '';
  10. $blog = isset( $_POST['tougao_authorblog'] ) ? trim(htmlspecialchars($_POST['tougao_authorblog'], ENT_QUOTES)) : '';
  11. $title = isset( $_POST['tougao_title'] ) ? trim(htmlspecialchars($_POST['tougao_title'], ENT_QUOTES)) : '';
  12. $category = $catidfankui ? (int)$catidfankui : 0;
  13. $content = isset( $_POST['tougao_content'] ) ? trim(htmlspecialchars($_POST['tougao_content'], ENT_QUOTES)) : '';// 表单项数据验证
  14. if ( empty($name) ) {
  15. wp_die('姓名必须填写。<a href="javascript:history.go(-1);">点此返回</a>');
  16. }
  17.  
  18. if ( empty($tell) || empty($dizhi) ) {
  19. wp_die('电话、地址都需要填写。<a href="javascript:history.go(-1);">点此返回</a>');
  20. }
  21.  
  22. if ( empty($email) || strlen($email) > 60 || !preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email)) {
  23. wp_die('Email必须填写,且长度不得超过60字,必须符合Email格式。<a href="javascript:history.go(-1);">点此返回</a>');
  24. }
  25.  
  26. if ( empty($title) ) {
  27. wp_die('标题必须填写。<a href="javascript:history.go(-1);">点此返回</a>');
  28. }
  29.  
  30. if ( empty($content) || mb_strlen($content) > 3000 || mb_strlen($content) < 20) {
  31. wp_die('内容必须填写,且长度不得超过3000字,不得少于20字。<a href="javascript:history.go(-1);">点此返回</a>');
  32. }
  33.  
  34. $post_content = '
  35. <p>姓名: '.$name.'</p>
  36. <p>电话: '.$tell.'</p>
  37. <p>Email: '.$email.'</p>
  38. <p>地址: '.$dizhi.'</p>
  39. <p>标题: '.$title.'</p>
  40. <p>内容:<br />'.$content.'</p>';
  41.  
  42. $tougao = array(
  43. 'post_title' => $title,
  44. 'post_content' => $post_content,
  45. 'post_category' => array($category)
  46. );
  47.  
  48. // 将文章插入数据库
  49. $status = wp_insert_post( $tougao );
  50.  
  51. if ($status != 0) {
  52. wp_die('提交成功!工作人员会尽快与您联系!<a href="javascript:history.go(-1);">点此返回</a>', '提交成功');
  53. }
  54. else {
  55. wp_die('提交失败!<a href="javascript:history.go(-1);">点此返回</a>');
  56. }
  57. }?>

第三步

上传用户提交表单页面到自己的服务器上。服务器需要支持 PHP 才可以。这样用户就可以在前台表单上填写信息提交了,提交后的信息会自动显示在管理员后台。

WordPress 怎么制作一个可留言的单网页

已有 208 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!