【发布时间】:2011-06-26 15:14:20
【问题描述】:
我有以下表格:
<fieldset id="upload-form">
<ul>
<li>
<div class="radio-list clearfix">
<ul>
<li><label for="r1"><input type="radio" name="category" id="r1" value="34" checked="checked" /> Discussion</label></li>
<li><label for="r2"><input type="radio" name="category" id="r2" value="35" /> Question</label></li>
<li><label for="r3"><input type="radio" name="category" id="r3" value="36" /> Code</label></li>
<li><label for="r4"><input type="radio" name="category" id="r4" value="37" /> Link</label></li>
</ul>
</div>
</li>
<li>
<label for="title">Title <span>required & make it descriptive</span></label>
<input id="title" type="text" name="title" />
</li>
<li>
<label for="url">URL <span>don't forget the http://</span></label>
<input id="url" type="text" name="url" placeholder="http://" />
</li>
<li>
<label for="topic">Discussion topic</label>
<textarea id="topic" name="topic"></textarea>
</li>
<li>
<label for="question">Your question <span>help text</span></label>
<textarea id="question" name="question"></textarea>
</li>
<li>
<label for="description">Description <span>help text</span></label>
<textarea id="description" name="description"></textarea>
</li>
<li>
<label for="code">Code</label>
<textarea id="code" name="code"></textarea>
</li>
<li>
<label for="tags">Tags <span>separate multiple tags with commas</span></label>
<textarea id="tags" name="tags"></textarea>
</li>
<li>
<label class="public" for="public"><input id="public" type="checkbox" name="public" /> Make this post public? <span>( non-members will be able to view this post )</span></label>
</li>
<li class="clearfix">
<input class="submit" type="submit" name="" value="Post" />
<input class="cancel" type="button" value="Cancel" onclick="window.location.href='<?php bloginfo('url'); ?>/posts'" />
</li>
</ul>
</fieldset>
当用户选择表单顶部的一个单选按钮时会发生什么情况,它将隐藏或显示与该类别相关的某些字段。例如,对于 Link 类别,唯一使用的字段将是 Title、URL 和 Description。
我打算使用 jQuery 来隐藏和显示字段,但是服务器端代码呢?好像用户开始填写字段但随后决定更改类别我不希望他们发布错误的字段等。我将如何完成此操作?
我使用的是 WordPress,表单是使用 PHP 编码的。这是表单如何获取帖子信息并将其保存到数据库的一些示例代码。谢谢。
<?php
/* Template Name: New Post */
if(isset($_POST['new_post']) == '1')
{
$post_title = $_POST['post_title'];
$post_category = $_POST['cat'];
$post_content = $_POST['post_content'];
$post_timelimit = $_POST['timelimit'];
$new_post = array(
'ID' => '',
'post_author' => $user->ID,
'post_content' => $post_content,
'post_title' => $post_title,
'post_status' => 'publish',
'tax_input' => array('timelimit' => $post_timelimit)
);
$post_id = wp_insert_post($new_post);
// tags have to be added seperate? :/
wp_set_post_tags($post_id, $_POST['post_tags']);
// This will redirect you to the newly created post
$post = get_post($post_id);
wp_redirect($post->guid);
}
get_header();
?>
【问题讨论】:
-
需要为表单更新 php,没有查看任何输入。 $_POST 应该与表单输入的名称匹配。
-
这是示例代码,我从未说过这是实际的表单代码!
标签: php javascript jquery wordpress