[英]After form submission want to show success message in the form page
管理員方面,我將表單提交到admin-post.php。 我想在表格底部打印成功消息。 我在wordpress中是新的,我在下面顯示了什么。
admin.php?page = add-products表單代碼
<form action="<?php echo admin_url('admin-post.php') ?>" method="post">
<table>
<input type="hidden" name="action" value="add_product_from_admin">
<tr><td>Name</td><td><input type="text" name="pr_name" id="pr_name"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="pr_submit" id="pr_submit" value="Save Products"></td></tr>
</table>
</form>
add_action( 'admin_post_add_product_from_admin', 'add_product_into_data_base' );
function add_product_into_data_base() {
//some database operations
wp_redirect(admin_url('admin.php?page=add-products&message=success'));
}
這與其說是WordPress,還不如說是PHP。 如果您不想隱藏表單中的任何內容,或者不想使用Ajax提交/處理表單,則只需檢查&message
參數是否存在並回顯它即可。
順便說一句,您應該謹慎處理縮進-必須清理掉這些東西。 這將使您的生活更輕松。 這是一個超級基本的示例,除非將&message
設置為查詢字符串參數,否則將不使用表單:
<form action="<?php echo admin_url('admin-post.php') ?>" method="post">
<table>
<input type="hidden" name="action" value="add_product_from_admin">
<input type="hidden" name="message" value="success">
<tr>
<td>Name</td>
<td><input type="text" name="pr_name" id="pr_name"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="pr_submit" id="pr_submit" value="Save Products"></td>
</tr>
</table>
<?= isset( $_POST['message'] ) ? $_POST['message'] : ''; ?>
</form>
此代碼將檢查在頁面加載期間(提交表單時)傳遞的數據中是否存在您隱藏的success
操作,並回顯“您的消息”。
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'success' ) {
echo 'Your Message';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.