Title: form-action.php
Last modified: November 16, 2020

---

# form-action.php

 *  [krniro](https://wordpress.org/support/users/krniro/)
 * (@krniro)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/form-action-php-2/)
 * Hi to all. Of course, I’m here because I’m facing issues with WordPress. 🙂
 * I’m struggling for quite some while now, to write from form to DB through custom
   made plugin. Here is my code.
 *     ```
       <?php
       //Add Calendar to DB
       function acr_addRoom() {
            if ( isset($_POST["name"]) && isset($_POST["maxUnits"]) && isset($_POST["partlyBooked"]) > 0){
   
       	 //get values from form
       	 $name    = sanitize_text_field($_POST["name"]);
            $maxUnits   = intval($_POST["maxUnits"]);
            $partlyBooked = intval($_POST['partlyBooked']);
   
       	//inert into DB
       	global $wpdb;
       	$acr_rooms_table = $wpdb->prefix.'acr_rooms';
       	$wpdb->insert(
                   $acr_rooms_table,
                   array(
                       'name'    => $name,
                       'maxUnits'   => $maxUnits,
                       'partlyBooked' => $partlyBooked
                   ));
       	}
       }
       add_action( 'admin_post_acr_addRoom', 'acr_addRoom' );
       ?>
   
       <div class="wrap">
        <h1>Add Room Info</h1>  
         <form method="post" action="admin-post.php">
         <input type="hidden" name="action" value="acr_addRoom" />
       	<table class="form-table">
       	  <tr>
       		<td><label for="name">Name</label></td>
       		<td align="left"><input name="name" placeholder="Room Type A" id="name" type="text" class="regular-text code" required />
       		</td>
       	</tr>
       	<tr>
       		<td><label for="maxUnits">Number of rooms</label></td>
       		<td align="left"><input placeholder="10" name="maxUnits" id="maxUnits" type="number" class="regular-text code" min="1" required />
       		</td>
       	</tr>
       	<tr>
       		<td><label for="partlyBooked">Threshold for "partly booked"</label></td>
       		<td align="left"><input placeholder="1" name="partlyBooked" id="partlyBooked" type="number" class="regular-text code" min="1" required />
       		</td>
       	</tr>
       	</table>
        <br />
        <input class="button button-primary" type="submit" value="Add Room" />
         </form>
       </div>
       ```
   
 * After I fill out the form I do not get insertion into database & page stops at
   admin-post.php. Does anybody know what do I do wrong?
 * Thanks.
 * BR.

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/form-action-php-2/#post-13680795)
 * If the user might not be logged in, you also need to hook “admin_post_nopriv_acr_addRoom”.
 * The form tag’s action attribute should be a full, absolute URL to admin-post.
   php. Relative URLs will fail under certain conditions.
 * If you haven’t, define WP_DEBUG as `true` in wp-config.php
 * None of these may solve your problem. Find out if your callback is even called
   by error logging a message. There’s not much to admin-post.php. It gets the “
   action” value and fires the related action. You can temporarily place debug code
   in the file to verify all is working as it should. If your callback is called,
   it apparently is working. Then do a detailed debug of your callback, verifying
   every variable and conditional is working as it should.
 * You can use the Query Monitor plugin to verify the insert query’s SQL is as it
   should be.
 *  Thread Starter [krniro](https://wordpress.org/support/users/krniro/)
 * (@krniro)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/form-action-php-2/#post-13697027)
 * [@bcworkz](https://wordpress.org/support/users/bcworkz/) thank you for reply.
   I think that the form is working & action is called. When I get redirected to
   blank post-admin.php I can see on Inspection screen, tab Network, that I have
   the data of my form.
    I also realized, that I’m missing redirect. I added redirect,
   but somehow I’m still landing on post-admin.php blank page. I have also tried
   with action attribute as full, even though I have WP_DEBUG as true, but without
   sucess.
 *     ```
       <?php
       //Add Calendar to DB
       function acr_addRoom() {
            if ( isset($_POST["name"]) && isset($_POST["maxUnits"]) && isset($_POST["partlyBooked"]) > 0){
   
       	 //get values from form
       	 $name    = sanitize_text_field($_POST["name"]);
            $maxUnits   = intval($_POST["maxUnits"]);
            $partlyBooked = intval($_POST['partlyBooked']);
   
       	//inert into DB
       	global $wpdb;
       	$acr_rooms_table = $wpdb->prefix.'acr_rooms';
       	$wpdb->insert(
                   $acr_rooms_table,
                   array(
                       'name'    => $name,
                       'maxUnits'   => $maxUnits,
                       'partlyBooked' => $partlyBooked
                   ));
       	}
       wp_redirect(  admin_url("admin.php?page=availability_calendar_rental_overview&setting=roomAdded"));
       		exit;
       }
       add_action( 'admin_post_acr_addRoom', 'acr_addRoom' );
       ?>
   
       <div class="wrap">
        <h1>Add Room Info</h1>  
         <form method="post" action="admin-post.php">
         <input type="hidden" name="action" value="acr_addRoom" />
       	<table class="form-table">
       	  <tr>
       		<td><label for="name">Name</label></td>
       		<td align="left"><input name="name" placeholder="Room Type A" id="name" type="text" class="regular-text code" required />
       		</td>
       	</tr>
       	<tr>
       		<td><label for="maxUnits">Number of rooms</label></td>
       		<td align="left"><input placeholder="10" name="maxUnits" id="maxUnits" type="number" class="regular-text code" min="1" required />
       		</td>
       	</tr>
       	<tr>
       		<td><label for="partlyBooked">Threshold for "partly booked"</label></td>
       		<td align="left"><input placeholder="1" name="partlyBooked" id="partlyBooked" type="number" class="regular-text code" min="1" required />
       		</td>
       	</tr>
       	</table>
        <br />
        <input class="button button-primary" type="submit" value="Add Room" />
         </form>
       </div>
       ```
   
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/form-action-php-2/#post-13698445)
 * Where are you actually placing your code? The action callback and add_action()
   call do not belong in the same place as your form HTML. If they are together 
   as they are here, the PHP portion may not be evaluated when admin-post.php is
   requested.
 *  Thread Starter [krniro](https://wordpress.org/support/users/krniro/)
 * (@krniro)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/form-action-php-2/#post-13699799)
 * Yes, you are right. This was an issue. I added function to external file & everything
   works.
    Thanks again!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘form-action.php’ is closed to new replies.

## Tags

 * [database](https://wordpress.org/support/topic-tag/database/)
 * [forms](https://wordpress.org/support/topic-tag/forms/)
 * [wpdb](https://wordpress.org/support/topic-tag/wpdb/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 2 participants
 * Last reply from: [krniro](https://wordpress.org/support/users/krniro/)
 * Last activity: [5 years, 4 months ago](https://wordpress.org/support/topic/form-action-php-2/#post-13699799)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
