Skip to main content

How to use properly search ajax in wordpress?

use properly search ajax in wordpress


Use in html file 

<html>
<head>

<style>
.sec {
    width: 223px;
    height: 41px;
    border: 1px solid #54595f;
margin-bottom:4px;
}
</style>
<script src="<?php echo plugin_dir_url(__FILE__);?>js/jquery.min.js" type="text/javascript"></script>
<script>
jQuery(document).ready(function($){
$(".filter").bind("keyup change", function() {


var ajaxurl = $(".ajaxurl").val();
var bylocation = $("#by-search").val();

var data = {
'action': 'showloactions',
'bylocation': bylocation,
};

jQuery.post(ajaxurl, data, function(response) {
$(".wrapper").html(response);
console.log(response);

});
});
});
</script>
</head>
<body>
<form action="" method="post">
<input type="text" name="search" class="filter" id="by-search">
<input type="hidden" class="ajaxurl" value="<?php echo admin_url( 'admin-ajax.php' ) ;?>">

</form>
<div class="wrapper">

</div>
<?php
//showloactions();

      ?>
</body>
</html>




Use in main plugin file 



 function showloactions(){

 $bylocation = $_POST['bylocation'];
 if(!$bylocation == ""){
        global $wpdb;
        global $wpsite;
$lacationtable = $wpdb->prefix.$wpsite."_locations";
        $result = $wpdb->get_results ( "SELECT * FROM $lacationtable where address LIKE '%$bylocation%'" );
        foreach ( $result as $print )   {
         
?>
<div class="sec"><?php echo $print->city; ?></div>
            <?php
}
}
else{

echo "Not Found";

}

die();

}
add_action('wp_ajax_showloactions', 'showloactions');
add_action('wp_ajax_nopriv_showloactions', 'showloactions');



Comments

Popular posts from this blog

How to check date format in PHP?

Check date format in PHP function isCorrectDateFromat($date){     if(!empty($date)){         $dateString = $date; // Replace this with your date string         $format = "Y-m-d"; // Replace this with your expected date format         $dateTime = DateTime::createFromFormat($format, $dateString);         if ($dateTime === false) { /*             echo "The date is not in the correct format."; */         } else {             $errors = DateTime::getLastErrors();             if (empty($errors)) { /*                 echo "The date is in the correct format."; */                 return true;             } else { /*                 echo "...