提交表单后没有重定向

时间:2013-09-16 13:30:36

标签: php jquery forms submit

首先是我的代码:

<?php
$postTitleError = '';
$postCompany = $_POST['postCompany'];
$postCompanyUrl= $_POST['postCompanyUrl'];  

if(isset($_POST['submitted']) && isset($_POST['post_nonce_field']) &&     wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {

if(trim($_POST['postTitle']) === '') {
    $postTitleError = 'Please enter a title.';
    $hasError = true;
} else {
    $postTitle = trim($_POST['postTitle']);
}


$post_information = array(
    'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
    'post_content' => esc_attr(strip_tags($_POST['postContent'])),
    'post_type' => 'opinie',
    'post_status' => 'pending'
);

$post_id = wp_insert_post($post_information);

add_post_meta($post_id, 'nazwa_firmy', $postCompany, true);
add_post_meta($post_id, 'url_firmy', $postCompanyUrl, true);

if ( $post_id ) {

}
}
?>

和html:

<form action="" method="POST" id="primaryPostForm" >
<div class="row-fluid">
    <div class="form-group span6">
            <label for="postTitle">Imię i nazwisko<span class="wymagane">*</span></label>
            <input type="text" name="postTitle" class="form-control required" id="postTitle" placeholder="Imię i nazwisko" value="<?php if(isset($_POST['postTitle'])) echo $_POST['postTitle'];?>">
    </div>
    <div class="form-group span6">
            <label for="postEmail">Adres e-mail<span class="wymagane">*</span></label>
            <input type="email" name="postEmail" class="form-control required" id="postTitle" placeholder="Twój e-mail" value="<?php if(isset($_POST['postTitle'])) echo $_POST['postTitle'];?>">
    </div>
<?php wp_nonce_field('post_nonce', 'post_nonce_field'); ?>
        <input type="hidden" name="submitted" id="submitted" value="true" />
<button type="submit" class="btn btn-primary">Dodaj</button>
</form>

我需要在不刷新页面的情况下发布此表单。我希望消失表格,并显示此表格已成功发送的消息。我不知道怎么做,我看了谷歌,但没有任何效果。

1 个答案:

答案 0 :(得分:0)

添加另一个div,表示使用display:none,提交即可,并添加onSubmit处理程序以隐藏表单。

粗略地说:

<form .... onSubmit="javascript:{getElementById('primaryPostForm').style.display='none';getElementById('myMessage').display='block';}">
</form>
<div id="myMessage" style="display:none">
      Submit OK
</div>