• Hello.

    I have made an custom admin-page where I have a form that I want to be able to add rows to a table in the database. The code is written in functions.php and I don’t know how to handle the data from the form so that I can use it with $wpdb->insert()

    I want to know how I can use a form in my custom admin page to add data to my custom table in the database, and do it all in functions.php.

    Here are my code for the admin page where I want my form to populate a table in my database:

    <?php
    // create custom plugin settings menu
    add_action('admin_menu', 'bk_retailers_create_menu');
    
    function bk_retailers_create_menu() {
    
    	//create new top-level menu
    	add_menu_page('Återförsäljare', 'Återförsäljare', 'administrator', __FILE__, 'bk_retailers_settings_page' , 'dashicons-tickets', 5 );
    
    	//call register settings function
    	add_action( 'admin_init', 'register_bk_retailers_settings' );
    }
    
    function register_bk_retailers_settings() {
    	//register our settings
    	register_setting( 'bk-retailers-settings-group', 'place_name' );
    	register_setting( 'bk-retailers-settings-group', 'address' );
    	register_setting( 'bk-retailers-settings-group', 'post_nr' );
    	register_setting( 'bk-retailers-settings-group', 'ort' );
    	register_setting( 'bk-retailers-settings-group', 'telefon' );
    	register_setting( 'bk-retailers-settings-group', 'hemsida' );
    	register_setting( 'bk-retailers-settings-group', 'epost' );
    	register_setting( 'bk-retailers-settings-group', 'lat' );
    	register_setting( 'bk-retailers-settings-group', 'lng' );
    	register_setting( 'bk-retailers-settings-group', 'landskap' );
    }
    
    function bk_retailers_settings_page() {
    ?>
    
    <div class="widefat">
    	<h2>Lägg till Återförsäljare</h2>
    
    	<form method="post" action="options.php">
    		<?php settings_fields( 'bk-retailers-settings-group' ); ?>
    		<?php do_settings_sections( 'bk-retailers-settings-group' ); ?>
    		<table class="form-table">
    			<tr valign="top">
    			<th scope="row">Namn</th>
    			<td><input type="text" name="place_name" value="<?php echo esc_attr( get_option('place_name') ); ?>" /></td>
    			</tr>
    
    			<tr valign="top">
    			<th scope="row">Adress</th>
    			<td><input type="text" name="address" value="<?php echo esc_attr( get_option('address') ); ?>" /></td>
    			</tr>
    
    			<tr valign="top">
    			<th scope="row">Post nummer</th>
    			<td><input type="text" name="post_nr" value="<?php echo esc_attr( get_option('post_nr') ); ?>" /></td>
    			</tr>
    
    			<tr valign="top">
    			<th scope="row">Ort</th>
    			<td><input type="text" name="ort" value="<?php echo esc_attr( get_option('ort') ); ?>" /></td>
    			</tr>
    
    			<tr valign="top">
    			<th scope="row">Telefon</th>
    			<td><input type="text" name="telefon" value="<?php echo esc_attr( get_option('telefon') ); ?>" /></td>
    			</tr>
    
    			<tr valign="top">
    			<th scope="row">Hemsida</th>
    			<td><input type="text" name="hemsida" value="<?php echo esc_attr( get_option('hemsida') ); ?>" /></td>
    			</tr>
    
    			<tr valign="top">
    			<th scope="row">Post nummer</th>
    			<td><input type="text" name="post_nr" value="<?php echo esc_attr( get_option('post_nr') ); ?>" /></td>
    			</tr>
    
    			<tr valign="top">
    			<th scope="row">Epost</th>
    			<td><input type="text" name="epost" value="<?php echo esc_attr( get_option('epost') ); ?>" /></td>
    			</tr>
    
    			<tr valign="top">
    			<th scope="row">lat</th>
    			<td><input type="text" name="lat" value="<?php echo esc_attr( get_option('lat') ); ?>" /></td>
    			</tr>
    
    			<tr valign="top">
    			<th scope="row">lng</th>
    			<td><input type="text" name="lng" value="<?php echo esc_attr( get_option('lng') ); ?>" /></td>
    			</tr>
    
    		</table>
    
    		<?php submit_button(); ?>
    
    	</form>
    </div>
    <?php } ?>

The topic ‘Submit form from custom admin-page’ is closed to new replies.