Showing posts with label Insert custom post data with custom field using ajax in wordpress. Show all posts
Showing posts with label Insert custom post data with custom field using ajax in wordpress. Show all posts

Wednesday, 16 March 2016

Insert custom post data with custom field using ajax in wordpress

Insert custom post data  with custom field using ajax in wordpress



<!-- This code in your custom template -->

<script>
 jQuery(document).ready(function() {
var form = jQuery( "#newCustomerForm" );


jQuery('.alu1').click(function(){

  var  checkForm = form.valid();
 
  if(checkForm != false){

var newCustomerForm = jQuery('#newCustomerForm').serializeArray();
jQuery.ajax({
url: "<?php bloginfo('wpurl') ?>/wp-admin/admin-ajax.php",
type:'POST',
data: newCustomerForm,
success: function(data){

if(data == "success"){


}
jQuery("form").trigger("reset");
}


});
}
return false;
});
 });
</script>
<form name="test" id="newCustomerForm">
                              <div class="input-group">
                              <span class="input-group-addon"><i class="fa fa-user"></i></span>
                              <input  class="form-control" type="text" placeholder="Enter your name" name="alu_name" required="required" />
                             
                              </div>
                              <div class="input-group">
                              <span class="input-group-addon"><i class="fa fa-envelope-o"></i></span>
                             
                                <input id="exampleInputEmail1" class="form-control" type="email" placeholder="Enter your Email" name="alu_email" required="required" />
                                </div>
                               
                                <div class="input-group">
                              <span class="input-group-addon"><i class="fa fa-mobile"></i></span>
                                <input  class="form-control" type="number" placeholder="Enter your Mobile no" name="alu_mobile" required="required" />
                                </div>
                               
                                <div class="input-group">
                              <span class="input-group-addon"><i class="fa fa-university"></i></span>
                                <input  class="form-control" type="text" placeholder="Enter your city name" name="alu_city" required="required" />
                                </div>
                               
                                <div class="input-group">
                              <span class="input-group-addon"><i class="fa fa-commenting"></i></span>
                                <textarea id="exampleTextarea" class="form-control" rows="3" name="alu_message" required="required"></textarea>
                                </div>
                                <button type="button" class="btn primary alu1">Submit</button>
                                 <input type="hidden" name="submitted" id="submitted" value="true" />
    <input type="hidden" name="nonce" value="<?php echo wp_create_nonce( 'form-nonce' );?>" />  
                            <input type="hidden" name="action" id="my_action" value="my_action" />                   
                                </form>
   
   
   
   
    <?php                           

/** In Your Function File **/
              function myFunction(){
   //do something
  
  
  
 
  

$alu_name=    sanitize_meta('name_alu_c1',$_POST['alu_name'],'user');
$alu_email=    sanitize_email($_POST['alu_email']);
$alu_mobile=    sanitize_meta('phone_alu_c1',$_POST['alu_mobile'],'user');
$alu_city=    sanitize_meta('city_alu_c1',$_POST['alu_city'],'user');
$alu_message=    sanitize_meta('message_alu_c1',$_POST['alu_message'],'user');

$my_cptpost_args = array(
          
            'post_title' => $alu_name,
            'post_status'   => 'publish',
            'post_type' => "alu_contact1",
);
$cpt_id = wp_insert_post($my_cptpost_args, $wp_error );
if($cpt_id){
add_post_meta($cpt_id, 'name_alu_c1', $alu_name, true);//here insert two custom field data.
add_post_meta($cpt_id, 'email_address_alu_c1', $alu_email, true);//here insert two custom field data.
add_post_meta($cpt_id, 'phone_alu_c1', $alu_mobile, true);//here insert two custom field data.
add_post_meta($cpt_id, 'city_alu_c1', $alu_city, true); //here insert two custom field data..
add_post_meta($cpt_id, 'message_alu_c1', $alu_message, true); //here insert two custom field data..

$test = wp_mail( $alu_email, "Contact Us", "Thank You For Contact us" );

echo "success";
}
       
       
   die();
}





add_action('wp_ajax_my_action', 'myFunction');           // for logged in user
add_action('wp_ajax_nopriv_my_action', 'myFunction');    // if user not logged in