jquery ajax form

jQuery(document).ready(function() {
jQuery(“.submit_customer_btn”).click(function() {
/**************** Register ***************************/
jQuery(“#notic_add_customer.alert”).hide();
//on focus
jQuery(“#add_customer_form .isrequired, #add_customer_form .isrequired”).focus(function(){
jQuery(this).removeClass(‘error’);
});
//on blur
jQuery(“#add_customer_form .isrequired, #add_customer_form .isrequired”).blur(function()
{
if(jQuery(this).val().trim() == ”)
{
jQuery(this).addClass(‘error’);
} else {
jQuery(this).removeClass(‘error’);
}
});
if(jQuery(“#firstname”).val().trim() == ” || jQuery(“#lastname”).val().trim() == ” || jQuery(“#mobile”).val().trim() == ” || jQuery(“#email”).val().trim() == ” || jQuery(“#streetaddress1″).val().trim() == ” || jQuery(“#zip”).val().trim() == ” || jQuery(“#city”).val().trim() == ” || jQuery(“#country”).val().trim() == ” ){
jQuery(“#notic_add_customer.alert”).show();
jQuery(“#notic_add_customer.alert”).addClass(“alert-danger”);
jQuery(“#notic_add_customer.alert”).html(“Please enter required field”);
jQuery(“#add_customer_form .isrequired” ).each(function() {
filedValue = jQuery(this).val().trim();
if(filedValue == ”)
{
jQuery(this).addClass(‘error’);
} else {
jQuery(this).removeClass(‘error’);
}
});
}else if (!ValidateEmail(jQuery(“#email”).val())) {
jQuery(“#notic_add_customer.alert”).show();
jQuery(“#notic_add_customer.alert”).addClass(“alert-danger”);
jQuery(“#notic_add_customer.alert”).html(“Please enter valid email address”);
jQuery(“#email”).addClass(‘error’);
} else {
var formData = jQuery(“#add_customer_form”).serialize();
jQuery.ajax({
type:”POST”,
url: frontendajax.ajaxurl,
data :formData+’&action=act_submit_customer’,
dataType: ‘JSON’,
success:function(data) {
if(data.status == ‘success’){
jQuery(“#notic_add_customer.alert”).show();
jQuery(“#notic_add_customer.alert”).addClass(“alert-success”);
jQuery(“#notic_add_customer.alert”).html(data.message);
//jQuery(“.CustPopup”).delay(500).fadeOut(“slow”);
// redirect to google after 2 seconds
window.setTimeout(function() {
window.location.href = ‘http://localhost/newmamamarked/customer/‘;
}, 2000);
} else if(data.status == ‘error’){
    jQuery(“#notic_add_customer.alert”).show();
jQuery(“#notic_add_customer.alert”).addClass(“alert-danger”);
jQuery(“#notic_add_customer.alert”).html(data.message);
}
},
});
return true;
}
return false;
});
});
function ValidateEmail(email) {
        var expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
        return expr.test(email);
};
/**************************************************/
php code

function act_ajax_scripts() {

wp_enqueue_script( ‘frontend-ajax’, get_stylesheet_directory_uri() . ‘/js/frontend-ajax.js’, array(‘jquery’));
wp_localize_script( ‘frontend-ajax’, ‘frontendajax’, array(
// URL to wp-admin/admin-ajax.php to process the request
‘ajaxurl’ => admin_url( ‘admin-ajax.php’ ),

));
}
add_action( ‘wp_enqueue_scripts’, ‘act_ajax_scripts’ );

/**
* New Customer registration
*
*/
function act_submit_customer(){

if(isset($_POST[‘action’]) == ‘submit_customer’){
global $wpdb;
$data=array(
’email’ => $_POST[’email’],
‘mobile’ => $_POST[‘mobile’],
‘picture_id’ => $_POST[‘pictureid’],
‘comment’ => $_POST[‘comments’],
‘bank_reg’ => $_POST[‘bankregno’],
‘bank_account’ => $_POST[‘bankaccno’],
‘nem_id’ => $_POST[‘nameid’],
‘company_name’ => $_POST[‘companyname’],
‘company_id’ => $_POST[‘companyid’],
‘phone’ => $_POST[‘phone’],
‘customer_role’ => $_POST[‘customer_role’],
‘default_language’ => $_POST[‘default_language’],
‘notify_economy’ => $_POST[‘notifications_economic_message’],
‘extend_auto’ => $_POST[‘extend_booking_automatically’],
‘general_discount_when_sale’ => $_POST[‘stall_sale_discount’],
‘notify_sales’ => $_POST[‘notifications_product_sales’],
‘notify_sales_when’ => $_POST[‘notifications_product_sales_when’],
‘price_policy’ => $_POST[‘price_policy’],
‘accept_minimum_price’ => $_POST[‘accept_minimum_price’],
‘general_discount_percentage’ => $_POST[‘discount_2_percentage’],
‘general_discount_stall_sale’ => $_POST[‘general_discount_percentage2’],
‘allow_images’ => $_POST[‘allow_upload_of_product_images’],
‘allow_extra_text’ => $_POST[‘allow_extra_product_text’],
‘force_english’ => $_POST[‘force_english_language’],
‘allow_google_translation’ => $_POST[‘allow_google_translation’],
‘force_stall_id’ => $_POST[‘choose_stall_id_per_stalls’],
‘force_category’ => $_POST[‘force_category’],
‘force_color’ => $_POST[‘force_color’],
‘force_size’ => $_POST[‘force_sizes’],
‘force_check_out’ => $_POST[‘force_product_checkout_after_expire’],
‘force_strict_units’ => $_POST[‘force_strict_units_in_stock’]
);

$wpdb->insert(‘mm_users_add’,$data);

$firstname = sanitize_text_field($_POST[‘firstname’]);

$lastname = sanitize_text_field($_POST[‘lastname’]);

$mobile = sanitize_text_field($_POST[‘mobile’]);

$email = sanitize_email($_POST[’email’]);

$companyname = !empty($_POST[‘companyname’]) ? sanitize_text_field($_POST[‘companyname’]):”;

$companyid = !empty($_POST[‘companyid’]) ? sanitize_text_field($_POST[‘companyid’]) :”;

$streetaddress1 = sanitize_text_field($_POST[‘streetaddress1’]);

$streetaddress2 = !empty($_POST[‘streetaddress2’]) ? sanitize_text_field($_POST[‘streetaddress2’]):”;

$zip = sanitize_text_field($_POST[‘zip’]);

$city = sanitize_text_field($_POST[‘city’]);

$country = sanitize_text_field($_POST[‘country’]);

$phone = !empty($_POST[‘phone’]) ? sanitize_text_field($_POST[‘phone’]):”;

$bankregno = !empty($_POST[‘bankregno’]) ? sanitize_text_field($_POST[‘bankregno’]) : ”;

$bankaccno = !empty($_POST[‘bankaccno’]) ? sanitize_text_field($_POST[‘bankaccno’]) : ”;

$comments = !empty($_POST[‘comments’]) ? sanitize_text_field($_POST[‘comments’]) :”;

$nameid = $_POST[‘nameid’];

$pictureid = $_POST[‘pictureid’];

//special field

$customer_role = sanitize_text_field($_POST[‘customer_role’]);

$default_language = sanitize_text_field($_POST[‘default_language’]);

$notifications_economic_message = sanitize_text_field($_POST[‘notifications_economic_message’]);

$notifications_product_sales = sanitize_text_field($_POST[‘notifications_product_sales’]);

$notifications_product_sales_when =sanitize_text_field($_POST[‘notifications_product_sales_when’]);

$extend_booking_automatically = sanitize_text_field($_POST[‘extend_booking_automatically’]);

$price_policy = sanitize_text_field($_POST[‘price_policy’]);

$accept_minimum_price = sanitize_text_field($_POST[‘accept_minimum_price’]);

$general_discount_percentage= !empty($_POST[‘discount_2_percentage’]) ? $_POST[‘discount_2_percentage’] : ”;

$general_discount_percentage_if_stall_sale = !empty($_POST[‘general_discount_percentage2’]) ? $_POST[‘general_discount_percentage2’] : ”;

$allow_upload_of_product_images = sanitize_text_field($_POST[‘allow_upload_of_product_images’]);

$allow_extra_product_text = sanitize_text_field($_POST[‘allow_extra_product_text’]);

$force_english_language = sanitize_text_field($_POST[‘force_english_language’]);

$allow_google_translation = sanitize_text_field($_POST[‘allow_google_translation’]);

$choose_stall_id_per_stalls = sanitize_text_field($_POST[‘choose_stall_id_per_stalls’]);

$force_category = sanitize_text_field($_POST[‘force_category’]);

$force_color = sanitize_text_field($_POST[‘force_color’]);

$force_sizes = sanitize_text_field($_POST[‘force_sizes’]);

$force_product_checkout_after_expire = sanitize_text_field($_POST[‘force_product_checkout_after_expire’]);

$force_strict_units_in_stock = sanitize_text_field($_POST[‘force_strict_units_in_stock’]);

$username = $firstname.’ ‘.$lastname;

$new_customer_data = array(
‘user_login’ => $email,
‘user_pass’ => $password,
‘user_email’ => $email,
‘user_nicename’ =>$username,
‘display_name’=>$username,
‘nickname’=>$username,
‘first_name’=>$firstname,
‘last_name’=>$lastname,
‘role’ => ‘customer’
);

$customer_id = wp_insert_user( $new_customer_data );

//echo “<pre>”;
//print_r($customer_id);

$extra_field=array(

‘user_id’ => $customer_id,

‘mobile’ => $mobile,

‘name_id_validated’ => $nameid,

‘picture_id’ => $pictureid,

‘bank_reg_number’ => $bankregno,

‘bank_account_number’ => $bankaccno,

‘comment’ => $comments,

‘customer_role’ => $customer_role,

‘default_language’ => $default_language,

‘notifications_economic_message’ =>$notifications_economic_message,

‘notifications_product_sales’ =>$notifications_product_sales,

‘notifications_product_sales_when’ => $notifications_product_sales_when,

‘extend_booking_automatically’=> $extend_booking_automatically,

‘price_policy’=> $price_policy,

‘accept_minimum_price’=> $accept_minimum_price,

‘general_discount_percentage’=>$general_discount_percentage,

‘general_discount_percentage_if_stall_sale’=> $general_discount_percentage_if_stall_sale,

‘allow_upload_of_product_images’=>$allow_upload_of_product_images,

‘allow_extra_product_text’=>$allow_extra_product_text,

‘force_english_language’=>$force_english_language,

‘allow_google translation’=>$allow_google_translation,

‘choose_stall_id_per_stalls’=>$choose_stall_id_per_stalls,

‘force_category’=>$force_category,

‘force_color’=>$force_color,

‘force_sizes’=>$force_sizes,

‘force_product_checkout_after_expire’=>$force_product_checkout_after_expire,

‘force_strict_units_in_stock’=>$force_strict_units_in_stock

);

$wpdb->insert(‘wps_user_extra’,$extra_field);

$user_data = get_user_by( ‘login’, $email );

$password_generated = wp_generate_password( 6, true, true );

$display_name = $_POST[‘firstname’].” “.$_POST[‘lastname’];
wp_update_user( array( ‘ID’ =>$_POST[‘ID’], ‘first_name’ =>$_POST[‘firstname’], ‘last_name’ =>$_POST[‘lastname’],’display_name’=>$display_name, ‘user_email’ =>$_POST[’email’]));
update_user_meta($_POST[‘ID’],”billing_address_1″,$_POST[‘streetaddress1’]);
update_user_meta($_POST[‘ID’],”billing_address_2″,$_POST[‘streetaddress2’]);
update_user_meta($_POST[‘ID’],”billing_postcode”,$_POST[‘zip’]);
update_user_meta($_POST[‘ID’],”billing_city”,$_POST[‘city’]);

$response_array = array( ‘status’ => ‘success’, ‘message’ => ‘Your Customer added successfully’ );

}

echo json_encode($response_array);
die();

}
add_action(‘wp_ajax_nopriv_act_submit_customer’, ‘act_submit_customer’);
add_action(‘wp_ajax_act_submit_customer’, ‘act_submit_customer’);

/*******************************/

// Add Shortcode
function act_customer_function() {

ob_start();

global $wpdb;

global $woocommerce;

global $wp_roles;

$countries_obj = new WC_Countries();

$countries = $countries_obj->__get(‘countries’);

$customers = $wpdb->get_results(“SELECT u.ID,u.display_name,u.user_email,m.meta_key,m.meta_value,e.mobile FROM wps1_users u JOIN wps1_usermeta m ON u.ID=m.user_id LEFT JOIN wps_user_extra e ON u.ID = e.user_id WHERE m.meta_key = ‘wps1_capabilities’

AND m.meta_value LIKE ‘%customer%’ ORDER BY u.user_registered DESC”);

//$wpdb->last_query;
$getID = $_REQUEST[‘getcustomer’];

//$customerSql = $wpdb->get_results( “SELECT * FROM mm_users_add WHERE id ='”.$getID.”‘”);

//$customerSql = “SELECT * FROM mm_users_add WHERE id ='”.$getID.”‘”;

//run any query
$customerSql = $wpdb->get_results( “SELECT * FROM mm_users_add ” );

 

}
add_shortcode( ‘customer-list’, ‘act_customer_function’ );
//[customer-list]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.