0
首先我的代碼:沒有重定向提交表單後
<?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>
我需要張貼這種形式而無需刷新頁面。我希望消失表格,並顯示一條消息,表明此表單已成功發送。我不知道如何做到這一點,我看過谷歌,但沒有任何工作。
您可以使用Ajax發佈形式和基於響應U可以顯示錯誤或成功的消息 – Bhadra