How to display success or failer message after WordPress form submited?

I have created a form in WordPress Plugin using general HTML tags. I just tested with echo for text box submitted value and it is working fine. I need to redirect or display success / failure message in same page after submitting form. How to display success or failure message after form submitted?

Related posts

2 comments

  1. <form method="post" role="form">
        <div class="form-group">
            <label for="txt">Enter Text</label>
            <input type="text" class="form-control" id="txt1" name="txt1" value="" placeholder="Enter your text..." />
        </div>
        <button type="submit" name="send" class="btn btn-default">Send</button>
    </form>
    
    <?php
        if(isset($_POST['send'])) {     
            $txt1 = trim($_POST['txt1']);
            $count=0;
            if(empty($txt1)) {
                echo '<div class="error">Please enter your text.</div>';
            } else {
                header("Location: http://www.yourwebsite.com/user.php");
                $count=1;
                if($count=1)
        { 
                    echo  '<label for="msg">Succes Message</label>';
                }
            }
        }
    ?>
    
  2. What worked for me is what follows:

    // After form save function ends do:
    wp_redirect($your_post_url . '&saved=1');
    exit;
    
    // Wherever you need the html do:
    if (isset($_GET['saved'])) echo 
      '<div class="updated">
        <p>' . __('Success! Values have been saved.') . '</p>
      </div>';
    

Comments are closed.