Script: run only if field value not exist
get_loc = jQuery('#autcomplete-location').val(); if(get_loc=='' && '<?php echo $_GET["action"]?>' == 'edit'){ jQuery.ajax({ url: '<?php echo admin_url('admin-ajax.php'); ?>', type: 'post', data: { action: 'get_location_redis', post_id: '<?php echo $_GET["post"]?>', }, success: function(data) { obj = JSON.parse(data); full_loc = obj.storyLocation; full_city = obj.city_name; full_loc_len = full_loc.length; //console.log(full_loc+' '+full_loc_len); if(full_loc!='' && full_loc_len>=1){ for (var i=0; i<full_loc.length; i++) { id_gen = (i>=1)?'-1':''; jQuery('#autcomplete-location'+id_gen).val(full_loc[i]); } }else if(full_city!='' && full_loc_len==1){ jQuery('#autcomplete-location').val(full_city); } }, error: function() { console.log("Error"); } }); }
AJAX
add_action('wp_ajax_get_location_redis' , 'get_location_redis'); add_action('wp_ajax_nopriv_get_location_redis','get_location_redis'); function get_location_redis(){ $post_id = $_REQUEST["post_id"]; if(!empty($storyLocation) || !empty($city_name)){ $storyLocation = $storyLocation; $city_name = $city_name; }else{ $storyLocation = ''; $city_name = ''; } $loc_data = array("storyLocation"=>$storyLocation, "city_name"=>$city_name); echo json_encode($loc_data); die(); }
Jagdish Sarma Asked question December 16, 2022