Javascript,Nodejs,MongodDB,WordPress,CSS,PHP

LightBlog

Tuesday, October 30, 2018

How to use ajax in wordpress

In wordpress ajax is working not like in core php ; it worked little bit different first it called its core function; in this case it use two hooks
do_action( "wp_ajax_nopriv_{$_REQUEST[‘action’]}" ):
Fires non-authenticated Ajax actions for logged-out users.
do_action( "wp_ajax_{$_REQUEST[‘action’]}" ):
Fires authenticated Ajax actions for logged-in users.
in this example i have created table cutom_cake_order
with cloumn name name,email,address,phone

<form action="" method="" id="myform">
                <fieldset>
                <legend>Personalia:</legend>
                       <div>
                           <input type="text" name="name" placeholder="Name">
                       </div>
                        <div>
                            <input type="text" name="email" placeholder="Email">   
                        </div>
                        <div>
                            <input type="text" name="address" placeholder="Address">
                        </div>
                        <div>
                             <input type="text" name="phone" placeholder="Phone">
                        </div>
                        <div>
                        <input type="hidden" name="action" value="addCustomer"/>       
                        <button type="submit" id="btn">Submit</button>
                        </div>
                </fieldset>
        </form>

<script>
   jQuery(document).ready(function() {
    jQuery("#btn").click(function(e){
     var jsonData = {};
   
   var formData = jQuery("#myform").serializeArray();
//    console.log(formData);
  
  jQuery.each(formData, function() {

        if (jsonData[this.name]) {
           if (!jsonData[this.name].push) {
               jsonData[this.name] = [jsonData[this.name]];
           }
           jsonData[this.name].push(this.value || '');
       } else {
           jsonData[this.name] = this.value || '';
       }
          
      
   });
   console.log(jsonData);
    var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
   jQuery.ajax(
    {
        url : ajaxurl,
        type: "POST",
        data : jsonData,
       
    });
    e.preventDefault();   
});
});
</script>
write this code inside functions file;

function.php:

function addCustomer(){

    global $wpdb;
   
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $address = $_POST['address'];
   
    $new_post = array(
        'post_title'   => 'cutom_cake_order',
        'post_content' =>'This is our custom cake order description',
        'post_type'    => 'shop_order',
    'post_status'  => 'open'          
    );
    $id = wp_insert_post($new_post);
    update_post_meta($id,'_amount',700);
   
    // if($wpdb->insert('customers',array(
    // 'name'=>$name,
    // 'email'=>$email,
    // 'address'=>$address,
    // 'phone'=>$phone
    // ))===FALSE){
   
    // echo "Error";
   
    // }
    // else {
    // echo "Customer '".$name. "' successfully added, row ID is ".$wpdb->insert_id;
   
    // }
    die();
}
    add_action('wp_ajax_addCustomer', 'addCustomer');
    add_action('wp_ajax_nopriv_addCustomer', 'addCustomer');

1 comment:


  1. Hello,

    we provide affordable and result-oriented SEO services, please give a chance to serve you.


    Thanks
    Admin: E07.net

    ReplyDelete