0

I have made one form on WP-admin side when i approved and reject the form working well but not sure why I'm not able to show my success Message on right place.

if i echo it working well but not right place. i want to show after my h2 tag below my codes.


<?php 
// Add a new menu item in the WordPress admin menu
function add_custom_menu_item() {
    add_menu_page(
        'Contracts Data',        // Page title
        'Contracts Data',        // Menu title
        'manage_options',        // Capability required
        'contracts-data',        // Menu slug
        'display_custom_menu'    // Callback function to display page content
    );
}
add_action('admin_menu', 'add_custom_menu_item');

/// Display custom admin page
function display_custom_menu() {
    $success_message = '';
    ?>
    <div class="wrap">
    <?php
        // Display success or error message
     if (!empty($success_message)) {
         echo '<div class="success-message">' . $success_message . '</div>';
     }
     ?>
        <h2>Contracts Data</h2>
        <table class="wp-list-table widefat striped">
            <thead>
                <tr>
                    <th>SL</th>
                    <th>Order Number</th>
                    <th>Order Status</th>
                    <th>User Email</th>
                    <th>Contract File</th>
                    <th>Contract Status</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <?php
                global $wpdb;
                $table_name = $wpdb->prefix . 'contracts';
                $contracts = $wpdb->get_results("SELECT * FROM $table_name", ARRAY_A);
                $cont=1;
                foreach ($contracts as $contract) {
                    $order = wc_get_order( $contract['order_number'] );
                    $order_status = $order ? $order->get_status() : 'Order not found';
                    echo "<tr>";
                    echo "<td>" . $cont++ . "</td>";
                    echo "<td>{$contract['order_number']}</td>";
                    echo "<td>{$order_status}</td>";
                    echo "<td>{$contract['user_email']}</td>";
                    echo "<td><a href='" . plugin_dir_url( __FILE__ ) . "{$contract['contract_file']}' target='_blank'>Download</a></td>";
                    echo "<td>{$contract['status']}</td>";
                    echo "<td>";
                    echo "<form method='post' action=''>";
                    echo "<input type='hidden' name='order_id' value='{$contract['order_number']}'>";
                    echo "<input type='hidden' name='user_email' value='{$contract['user_email']}'>";
                if($contract['status']=='approved'){
                    echo "<button type='submit' name='action' value='reject' class='btn btn-danger'>Reject</button>";
                } else{
                    echo "<button type='submit'name='action' value='approved' class='btn btn-success'>Approve</button>";
                }
                    echo "</form>";
                    echo "</td>";
                    echo "</tr>";
                }
                ?>
            </tbody>
        </table>
    </div>
    <?php
}

function update_contract_status( $order_id, $status ) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'contracts';
    
    $wpdb->update(
        $table_name,
        array( 'status' => $status ),
        array( 'order_number' => $order_id ),
        array( '%s' ),
        array( '%d' )
    );
}
// Handle form submission
function handle_contract_submission_app() {
    if( isset( $_POST['order_id'] ) && isset( $_POST['action'] ) ) {
        $order_id = intval( $_POST['order_id'] );
        $action = $_POST['action'];
       

        if ( $action === 'approved' ) {
            // Update contract status in the contract table
            update_contract_status( $order_id, 'approved' );
            $success_message = 'Contract status updated to Approved successfully!';
           
        } elseif ( $action === 'reject' ) {
            update_contract_status( $order_id, 'rejected' );
            $success_message = 'Contract status updated to Rejected successfully!';
            //echo '<div class="success-message">Contract status updated to Rejected successfully!</div>';
        }
    }
}
add_action('admin_init', 'handle_contract_submission_app');

Help Me to find my error thanks

4
  • 1
    "i want to show after my h2 tag below my codes" -- You've put it above the <h2> tag; move the <?php {success message code} ?> to where you want it in the HTML. Also, you're setting $success_message='' and then not setting it to anything else, so when the display code happens, your $success_message is still empty.
    – Pat J
    Feb 17 at 16:35
  • should i remove this-- $success_message=''
    – Atiq Rev
    Feb 29 at 11:49
  • If you want to display a message there, you'll need to set $success_message to something. It's not pre-populated, and currently your code is setting it to '', which is empty.
    – Pat J
    Feb 29 at 13:55
  • HI, i made this because if i don't have message then not show error. So, should i avoid this line?
    – Atiq Rev
    Feb 29 at 14:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.