Form submits but results not showing
-
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 } }
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Form submits but results not showing’ is closed to new replies.