• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Search

PHP

Ajax Live Search

May 22, 2022

Just implemented an Ajax live search on a website.

Here’s the search page. Results are displayed in div output.

<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#search").keyup(function(){
        var query = $(this).val();
        if (query != "") {
        $.ajax({
            url: 'links-ajax.php',
            method: 'POST',
            data: {query:query},
            success: function(data){
            $('#output').html(data);
            $('#output').css('display', 'block');
            $("#output a").on("click", function(){
                $("#output").val($(this).html());
            });
            $("#search").focusout(function(){
                window.setTimeout(function() {
                $('#output').css('display', 'none');
                }, 500);
            });
            $("#search").focusin(function(){
                $('#output').css('display', 'block');
            });
            }
        });
        } else {
        $('#output').css('display', 'none');
    }
    });
});
</script>
 
<div class="form-row">
    <div class="col">
        <input type="text" name="search" id="search" autocomplete="off" placeholder="Search here...." class="form-control form-control-success">
    </div>
</div>
<div id="output"></div>

<script src="https://code.jquery.com/jquery-3.4.1.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#search").keyup(function(){ var query = $(this).val(); if (query != "") { $.ajax({ url: 'links-ajax.php', method: 'POST', data: {query:query}, success: function(data){ $('#output').html(data); $('#output').css('display', 'block'); $("#output a").on("click", function(){ $("#output").val($(this).html()); }); $("#search").focusout(function(){ window.setTimeout(function() { $('#output').css('display', 'none'); }, 500); }); $("#search").focusin(function(){ $('#output').css('display', 'block'); }); } }); } else { $('#output').css('display', 'none'); } }); }); </script> <div class="form-row"> <div class="col"> <input type="text" name="search" id="search" autocomplete="off" placeholder="Search here...." class="form-control form-control-success"> </div> </div> <div id="output"></div>

Here’s the links-ajax.php which display the search results with links.

<?php
$servername='localhost';
$username='';
$password='';
$dbname = "";
$conn=mysqli_connect($servername,$username,$password,"$dbname");
if(!$conn) { die('Could not Connect MySql Server:' .mysql_error()); }
if (isset($_POST['query'])) {
    $query = <<<"SQL
                 SELECT * FROM links WHERE `name` LIKE '%{$_POST['query']}%' 
                 OR `url` LIKE '%{$_POST['query']}%' 
                 OR `tag` LIKE '%{$_POST['query']}%' 
                 ORDER BY name ASC, url ASC"
             SQL;
    $result = mysqli_query($conn, $query);
    if (mysqli_num_rows($result) > 0) {
        while ($user = mysqli_fetch_array($result)) {
            echo "<a target='_blank' rel="noopener">".$user['name']."</a><br/>";
        }
    } else {
        echo "<p style='color:red'>No results found...</p>";
    }
}
?>

<?php $servername='localhost'; $username=''; $password=''; $dbname = ""; $conn=mysqli_connect($servername,$username,$password,"$dbname"); if(!$conn) { die('Could not Connect MySql Server:' .mysql_error()); } if (isset($_POST['query'])) { $query = <<<"SQL SELECT * FROM links WHERE `name` LIKE '%{$_POST['query']}%' OR `url` LIKE '%{$_POST['query']}%' OR `tag` LIKE '%{$_POST['query']}%' ORDER BY name ASC, url ASC" SQL; $result = mysqli_query($conn, $query); if (mysqli_num_rows($result) > 0) { while ($user = mysqli_fetch_array($result)) { echo "<a target='_blank' rel="noopener">".$user['name']."</a><br/>"; } } else { echo "<p style='color:red'>No results found...</p>"; } } ?>

I couldn’t get the links working, until I added the 500ms of timeout in .focusout.

Filed Under: HTML, PHP Tagged With: ajax, live, search, web

PHP Modules

May 7, 2020

Here are some missing PHP modules on my WordPress install.

apt install php-gd php-dom php-mbstring php-imagick php-zip

apt install php-gd php-dom php-mbstring php-imagick php-zip

Reboot Apache after the install.

systemctl restart apache2

systemctl restart apache2

Filed Under: PHP, WP Tagged With: dom, gd, imagick, mbstring, modules, zip

List Available PHP Modules

March 2, 2020

Here’s how to list the available PHP modules.

apt-cache search php- | less

apt-cache search php- | less

To get details about a module.

apt-cache show php-curl

apt-cache show php-curl

To install a module.

apt install php-curl

apt install php-curl

Or install all modules.

apt install php*

apt install php*

Restart Apache after each install.

systemctl restart apache2

systemctl restart apache2

Filed Under: Linux, PHP Tagged With: install, modules, php

Phpinfo Blank Page

September 10, 2019

Here’s how to fix when phpinfo is displaying a blank page. The install is missing this php module.

apt install php libapache2-mod-php

apt install php libapache2-mod-php

Restart after install.

service apache2 restart

service apache2 restart

Filed Under: Linux, PHP Tagged With: blank, page, php, phpinfo

HTTPS in CodeIgniter

July 17, 2019

If you switched to HTTPS in Apache, make sure to update CodeIgniter’s config file.

vim /var/www/applications/config/config.php

vim /var/www/applications/config/config.php

Change the base URL to https.

$config['base_url']	= 'https://yourdomain.com/ci/';

$config['base_url'] = 'https://yourdomain.com/ci/';

Filed Under: PHP Tagged With: apache, base url, codeigniter, config, https, php

  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Interim pages omitted …
  • Go to page 15
  • Go to Next Page »
  • Home
  • About
  • Search

Copyright © 2023