我知道插入数据的正确方法是使用AJAX,但是我不介意页面刷新。页面刷新后,有人可以帮我清除表单数据吗?一切正常,正在将数据提交到表,然后页面刷新。但是,如果我再次按下刷新按钮,它会告诉我数据将被提交...而我不知道该怎么办。
<?php
$current_user = wp_get_current_user();
if ( $current_user->ID == 0 ) {
} else {
if( isset( $_POST['drop_artists'] ) ) {
$answer = $_POST['drop_artists'];
}else{
$answer = $_POST['artist_name'];
}
$date = current_time( 'mysql' );
$table = "t4t5_answers";
$sql = $wpdb->prepare( "INSERT INTO $table (user_id, post_id, info, answer, submission_date ) VALUES ( %d, %d, %s, %s, %d )", $current_user->ID, $post->ID, 'artist', $answer, $date );
$wpdb->query($sql);
header('Location: ' . get_bloginfo('url'));
}//if(isset($_POST['form_sub']))
?>
<form method="post" action="" id="artists-form">
<ul>
<li id="categories">
<?php
$args = array(
'show_option_all' => 'Artists',
'hierarchical' => 1,
'child_of' => 406,
'order_by' => 'name',
'name' => 'answer',
'hide_empty' => 0
);
wp_dropdown_categories($args); ?>
</li>
</ul>
<input type="text" name="artist_name" value="" size="45" id="input-title"/>
<input id="submitButton" class="subput" type="submit" name="submit" value="Add"/>
</form>
发布页面后,使用重定向将用户发送到成功页面。这将使实际发布的页面不会出现在其历史记录中。
if (!empty($_POST)) {
// do stuff
header("Location: $_SERVER[PHP_SELF]");
}
请注意,重定向的URL应该是完整的URL,而不是相对的URL。不过在实践中,我还没有看到任何浏览器的相对URL有问题。
要立即重定向到您当前的网址,请尝试使用此网址
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$current_URL.'">'
有关更多详细信息,请参见http://en.wikipedia.org/wiki/Meta_refresh
在if(isset($ _ POST ['submit'])中写您的php代码,并在insert语句下面添加以下脚本。
echo " <script type='text/javascript'>
window.location=document.location.href;
</script>";
您的代码将如下所示:
global $wpdb,$post;
if(isset($_POST['submit']))
{
$current_user = wp_get_current_user();
if ( $current_user->ID == 0 )
{
}
else
{
if( isset( $_POST['drop_artists'] ) )
{
$answer = $_POST['drop_artists'];
}
else
{
$answer = $_POST['artist_name'];
}
$date = current_time( 'mysql' );
$table = "t4t5_answers";
$sql = $wpdb->prepare( "INSERT INTO $table (user_id, post_id, info, answer, submission_date ) VALUES ( %d, %d, %s, %s, %d )", $current_user->ID, $post->ID, 'artist', $answer, $date );
$wpdb->query($sql);
}
echo "<script type='text/javascript'>
window.location=document.location.href;
</script>";
}
?>
<form method="post" action="" id="artists-form">
<ul>
<li id="categories">
<?php
$args = array(
'show_option_all' => 'Artists',
'hierarchical' => 1,
'child_of' => 406,
'order_by' => 'name',
'name' => 'answer',
'hide_empty' => 0
);
wp_dropdown_categories($args); ?>
</li>
</ul>
<input type="text" name="artist_name" value="" size="45" id="input-title"/>
<input id="submitButton" class="subput" type="submit" name="submit" value="Add"/>
</form>
这将在表单提交后刷新您的页面,并且不会重新提交表单值。