Sarathlal N

Handling form using AJAX in WordPress

As a WordPress developer, regularly I need to use forms in WordPress front end & admin pages. I like to use AJAX for form submissions because it will avoid page redirection & AJAX improves user interactions.

So here is the HTML, Javascript & PHP code collection that I use normally in my plugins. Sorry, there is no detailed explanation.

HTML FORM

<form name='sample_form' method='post' id='sample_form'>
    <p>
	    <label for="user_name">User name</label>
	    <input type='text' name="user_name" />
    </p>
    <p>
	    <input type='submit' class='button' value='submit' />
	    <input type="hidden" name="action" value="prefix_your_action_name">
	    <?php wp_nonce_field( 'name_of_my_action', 'name_of_nonce_field' ); ?>	
    </p>
</form>

You have to display the form in WordPress pages using proper hooks.

JavaScript / jQuery

<script type="text/javascript" >
	(function($, window, document) {
		$('#sample_form').submit(function(e){
			e.preventDefault();
			var formData = $(this).serializeArray();
			$.ajax({
				type: 'POST',
				url : ajaxurl, // `ajaxurl` only available in admin end
				data: formData,
				beforeSend : function(){
					// action before submit
				},
				success: function(response){
					// On success
				},
				error: function(jqXHR, textStatus, errorThrown){
					// On error
				},
				complete: function(jqXHR, textStatus){
					// On complete
				}
			});
		});
	}(window.jQuery, window, document));
</script>

PHP / WordPress

// Fires authenticated Ajax actions for logged-in users.
add_action( 'wp_ajax_prefix_your_action_name', 'your_callback_function' );

// Fires non-authenticated Ajax actions for logged-out users.
add_action( 'wp_ajax_nopriv_prefix_your_action_name', 'your_callback_function' );

function your_callback_function(){
    if(!isset($_REQUEST['name_of_nonce_field']) || !(wp_verify_nonce($_REQUEST['name_of_nonce_field'], 'name_of_my_action'))){
       wp_die('error!');
    }else{
       // process form data
    }

    wp_send_json($your_result);
}

Looking for a skilled WordPress/WooCommerce developer? I'm currently available for freelance, contract, or full-time remote opportunities! Let's create something amazing together. Send me a quick message, and I'll respond within 24 hours!

Recent Posts

  1. Automating Code Linting with GitHub Actions for WordPress Plugins
  2. Comprehensive Guide to Linting PHP, JavaScript, and CSS in WordPress Plugins Using Composer
  3. The Ultimate Guide to Indexing in Database Design
  4. Understanding 'update_meta_cache' in WordPress - When to Use It, When Not to, and Its Impact on Database Queries
  5. A Guide to Configuring JavaScript and SCSS Paths in WordPress Plugins with @wordpress/scripts

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.