• I am trying to write my first plugin based on code I picked up from around the web. The plugin installs and activates and has a page with a form. When I click the submit button on the form the page reloads. But the code handling the form is supposed to respond with a success or failure message in the url. It doesn’t do that. I am not seeing any errors. There are three files. The main file, a class file that has the form and a function file that has the form handling code. Would someone point out the mistake, please?

    /**
    Plugin Name: My Viewer
    **/
    /*** MAIN PAGE ***/
    
    add_action( 'admin_menu' , 'My_viewer');
    
    function My_viewer(){
        $page_title = 'My_viewer';
        $menu_title = 'My_viewer';
        $capability = 'manage_options';
        $menu_slug  = 'my_viewer';
        $function   = 'my_functions';
    
        add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function );
        add_submenu_page( $menu_slug, $page_title, 'Panel', 'manage_options', 'my-panel-submenu-page', 'my_panel_page' );
    }
    
    function my_functions(){
       require_once plugin_dir_path( __FILE__ ) . '/includes/my_functions.php';
    }
    function my_panel_page() {
        require_once dirname( __FILE__ ) . '/includes/class-my-class.php';
        $page = new My_class( plugin_basename( __FILE__ ) );
    
    ?>
      <div class="wrap">
       <h1><?php esc_html_e( 'Main Panel', 'access-log-viewer' ); ?></h1>
       <?php $page->DisplayPanel(); ?>
      </div>
    <?php
    }
    
    
    /*** FUNCTIONS FILE ***/
    
    function traitement_formulaire_don_cagnotte() {
     if ( ! isset( $_POST['cagnote-don-envoi'] ) || ! isset( $_POST['cagnotte-verif'] ) )  {
      return;
     }
    
     if ( ! wp_verify_nonce( $_POST['cagnotte-verif'], 'faire-don' ) ) {
      return;
     }
    
     $don = intval( $_POST['don'] );
     $url = wp_get_referer();
    
     // Donation amount is too low.
     if ( $don < 0 ) {
      $url = add_query_arg( 'erreur', 'radin', wp_get_referer() );
    
     // Donation amount is too high.
     } elseif ( $don > 10000 ) {
      $url = add_query_arg( 'erreur', 'trop', wp_get_referer() );
    
     // Everything's OK, let's do the work...
     } else {
      $cagnotte_actuelle = intval( get_option( 'valeur_cagnotte', 0 ) );
      update_option( 'valeur_cagnotte', $cagnotte_actuelle + $don );
      $url = add_query_arg( 'success', 1, wp_get_referer() );
     }
    
     // Redirect user back to the form, with an error or success marker in $_GET.
     wp_safe_redirect( $url );
     exit();
    }
    add_action( 'template_redirect', 'traitement_formulaire_don_cagnotte' );
    
    /*** CLASS FILE ***/
    
    class My_class {
      public function __construct( $plugin_basename ) {
      }
      
      public function DisplayPanel() {
        if ( isset( $_GET['erreur'] ) ) {
         $error = sanitize_title( $_GET['erreur'] );
    
         switch ( $error ) {
          case 'radin' :
           $message = __( 'We need a positive amount.', 'msk' );
           break;
    
          case 'trop' :
           $message = __( 'Thanks, but we do not need that much money.', 'msk' );
           break;
    
          default :
           $message = __( 'Something went wrong.', 'msk' );
           break;
         }
    
         printf( '<div class="error"><p>%1$s</p></div>', esc_html( $message ) );
        }
        ?>
        <form action="<?php echo esc_url( $_SERVER['REQUEST_URI'] ); ?>" method="POST" class="comment-form">
         <?php wp_nonce_field( 'faire-don', 'cagnotte-verif' ); ?>
    
         <div>
          <label for="don"><?php _e( 'Amount donation' ); ?></label>
          <input id="don" type="number" name="don" value="5" />
         </div>
    
         <input id="submit" type="submit" name="cagnote-don-envoi" id="submit" class="submit" value="<?php esc_attr_e( 'Submit', 'msk' ); ?>" />
        </form>
          <?php
          ?> <pre> <?php
        print_r($_GET);
        ?> </pre> <?php
      }
    }
    • This topic was modified 1 year, 11 months ago by wpusrpt.
Viewing 4 replies - 1 through 4 (of 4 total)
  • function traitement_formulaire_don_cagnotte() {
     if ( ! isset( $_POST['cagnote-don-envoi'] ) || ! isset( $_POST['cagnotte-verif'] ) )  {
      return;
     }

    The first thing your form process does is check these POST arguments

    But you are the redirecting back by appending a GET argument, and the POST arguments will be lost

    You need to redirect back appending both cagnote-don-envoi and cagnotte-verif and ‘don’ to the get and when you check use $_REQUEST which will handle both $_POST and $_GET args

    Of course this is a horrible user experience and if you are really only validating don amount why not

    <input id="don" type="number" name="don" value="5" min="0" max="1000" />

    If however you are using this as a starting point for a more complex experience, then consider javascript to handle the front end processing and ajax ( or REST_API ) for any dynamic server side action.

    • This reply was modified 1 year, 11 months ago by Alan Fuller.
    Thread Starter wpusrpt

    (@wpusrpt)

    Thank you for your reply. I changed the print_r to use $_REQUEST but all that it shows are the form elements. I don’t think the function to handle the form is ever being called. I found the admin_post action in the docs that sounds like it was for this purpose. In the class above the submit button I added

    <input type="hidden" name="action" value="my_panel_action" />

    and in the main file after calling my functions file I added

    add_action( 'admin_post_my_panel_action', 'traitement_formulaire_don_cagnotte' );
    

    But the above didn’t make any difference. Did I use that code correctly? How does the form handler get called?

    Regarding this code, it is just an example I found on the web. I’m only using it to learn how to handle the form.

    it is really difficult to tell from the posted snippets.

    What I suggest is you commit the complete plugin to a GitHub public repository and that way far easier to see what it is trying to do.

    but you probably are right in that the original form seems to be in an admin page, yet the post examination is done on template redirect which is a front end hook and so should be on an admin hook.

    Thread Starter wpusrpt

    (@wpusrpt)

    Thanks again for the suggestions. I have done away with the functions file and moved the form handler to the class and it is working now. I’m sure the function could be be made to work but this probably makes more sense in the long run.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Form submits but results not showing’ is closed to new replies.