• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

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

CodeIgniter 2.1.3 on PHP7

March 18, 2019

If you’re still running older versions of CodeIgniter, here are a couple of tips to make it work with PHP7.

  1. Make sure to install all PHP tools and libraries that comes with PHP7. I was missing curl.
  2. Change your database driver from mysql to mysqli (application/config/config.php).
  3. Fix the /system/core/Common.php error or as some prefer, upgrade to a newer version.

Considering v2.1.3 is very old (currently 3.1.10) , I’m surprised it works with PHP7 with just a few modifications.

Filed Under: PHP Tagged With: codeigniter, curl, mysqli, php7

Comparing Time

May 9, 2017

Here’s a neat little PHP script that compares a set date/time to the current time. The time() function sets the $now variable to the current Unix timestamp. The strtotime() function converts a date string to a Unix timestamp. The result is assigned to the $setdate variable. The two variables containing Unix timestamps are then compared. A simple if-then statement determines if the date is in the past or future.

date_default_timezone_set('America/Los_Angeles');
$setdate = strtotime("May 9, 2017 12:27PM");
$now = time();
if ( $setdate > $now ) : echo 'Future date'; else : echo 'Past date'; endif;

date_default_timezone_set('America/Los_Angeles'); $setdate = strtotime("May 9, 2017 12:27PM"); $now = time(); if ( $setdate > $now ) : echo 'Future date'; else : echo 'Past date'; endif;

You may have to change the default timezone if your server is in a different timezone.

Filed Under: PHP Tagged With: comparison, date, time, unix timestamp

Quick Database Check

March 17, 2017

Here’s a quick PHP script to check if your web server can connect to your MySQL database. It’s a neat little script if you don’t have any application installed yet, and you just want to see if your web server setup can quickly connect to the database. All you have to do is create a new file called connect.php, and place the following code inside. Just change the username, password and database. Most likely the hostname will be localhost.

$ sudo nano /var/www/html/connect.php
$username = "user";
$password = "password";
$hostname = "hostname";
$database = "database";
 
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
echo "Successfully Connected to the MySQL Database.<br/>";
 
$selected = mysql_select_db("$dbname", $dbhandle) or die("Could not select database");
echo "Successfully Connected to Database called: $database";

$ sudo nano /var/www/html/connect.php $username = "user"; $password = "password"; $hostname = "hostname"; $database = "database"; $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); echo "Successfully Connected to the MySQL Database.<br/>"; $selected = mysql_select_db("$dbname", $dbhandle) or die("Could not select database"); echo "Successfully Connected to Database called: $database";

It’s also available on my Github gist.

Filed Under: PHP Tagged With: connect, mysql

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

Copyright © 2023