How to create admin ajax function for my contact form

I am quite new to php, my website theme uses admin ajax to send enquiry form but there is no corresponding functions in admin-ajax.php. I want to know how create them. page which uses the contact form has this code

if ( isset( $cs_post_request_form_option ) && $cs_post_request_form_option == 'on' ) {
                    cs_user_conatct_form( $organizerID );
                }
                ?>

and on functions page i have the code for cs_user_conatct_form() which is

if(!function_exists('cs_user_conatct_form')){
function cs_user_conatct_form( $organizerID = ''){
    $user_contact_form  = get_the_author_meta( 'user_contact_form', $organizerID );
    if( $user_contact_form == '1') {
    ?>
    <div class="csuser_info contactform">
        <div id="cs_requestdetail">
          <div class="rq-form">
           <h5><?php esc_html_e('enquire now','directory');?></h5>

            <div id="request-loading"></div>
            <div class="request-message-type succ_mess" style="display:none"><p></p></div>  
            <form id="frm_request" name="frm_request" method="post" novalidate>
            <ul>
                <li><div class="rqform-text"><input type="text" placeholder="<?php _e('Name','directory');?>" name="<?php _e('request name','directory');?>" id="request_name" /></div></li>
                <li><div class="rqform-text"><input type="text" placeholder="<?php _e('Email','directory');?>" name="<?php _e('request email','directory');?>" id="request_email"></div></li>
                <li><div class="rqform-text"><input type="text" placeholder="<?php _e('Phone No','directory');?>" name="<?php _e('request number','directory');?>" id="request_number"></div></li>
                <li><div class="rqform-text"><textarea placeholder="<?php _e('Message','directory');?>" name="<?php _e('request message','directory');?>" id="request_message" ></textarea>
                     <ul class="check-box" style="display:none">
                        <li>
                            <div class="checkbox">
                                <input type="checkbox" id="checkbox2" checked="checked" name="<?php _e('term condtions check','directory');?>">
                            </div>
                        </li>
                     </ul>
                        <input type="hidden" name="user_id" value="<?php echo intval( $organizerID );?>" />
                        <input type="hidden" name="action" value="cs_add_request" />
                        <input type="button" value="<?php _e('Submit','directory');?>" class="cs-bgcolor" onclick="cs_request_submission('<?php echo admin_url('admin-ajax.php')?>', '<?php echo get_template_directory_uri()?>',<?php $("#frm_request")[0].reset();?>);">

                    </div>
                </li>
            </ul>
           </form>
          </div>
      </div>
    </div>
    <?php
    }
}

}

and on .js i have following code

    function cs_request_submission(admin_url,theme_url){
    'use strict';
    jQuery(".request-message-type").hide();
    jQuery("#request-loading").html("<img src='"+theme_url+"/include/assets/images/ajax_loading.gif' />");
    jQuery(".request-message-type").html('');
    jQuery.ajax({
        type:"POST",
        url: admin_url,
        dataType: "json",
        data:jQuery('#frm_request').serialize(), 
        success:function(response){
            if (response.type == 'success' ) {
                jQuery("#request-loading").html('');
                jQuery(".request-message-type").html(response.message);
                jQuery(".request-message-type").show();
                jQuery("#request_name").val('');
                jQuery("#request_email").val('');
                jQuery("#request_number").val('');
                jQuery("#request_message").val('');
                jQuery('#checkbox2').attr('checked', false); // Unchecks it
                jQuery("#frm_request").slideUp();

            } else {
                jQuery("#request-loading").html('');
                jQuery(".request-message-type").html(response.message);
                jQuery(".request-message-type").show();
            }
        }
    });
    return false;
}

I want to know how to create an admin-ajax function and how to send the page url like mydomain.com/adenquiry/postname on which the form is available.

Read more here: How to create admin ajax function for my contact form

Leave a Reply

Your email address will not be published. Required fields are marked *