简体   繁体   中英

After form submission want to show success message in the form page

Admin side I submit form to admin-post.php. I want to print success message in the bottom of the form. Iam new in wordpress what i did shown below.

admin.php?page=add-products form code

<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'));
}

This isn't so much a WordPress thing as a PHP thing. If you don't want to hide anything in the form, or submit/process the form with Ajax, you can just check to see if the &message parameter exists and echo it.

Also as an aside, you should be careful with your indentations - gotta clean that stuff up. It will make your life easier down the road. Here's a super basic example that will leave the form alone, unless &message is set as a query string parameter:

<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>

This code will check if your hidden success action is present in data passed during page load (any time the form is submitted), and echo "Your Message".

if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'success' ) {
    echo 'Your Message';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM