Code has been added to clipboard!

PHP - AJAX and PHP. Example #3

Example
<?php
    // Array of suggestable names
    $n = ['Anna', 'Brittany', 'Cinderella',
        'Diana', 'Fiona', 'Gunda', 'Inga',
        'Johanna', 'Linda', 'Nina', 'Ophelia',
        'Amanda', 'Cindy', 'Evie', 'Sunnivia',
        'Tovie', 'Violet', 'Elizabeth', 'Ellen', 'Vicky'];

    // get the value for $r from the URL
    $r = $_REQUEST['r'];

    $h = '';

    // lookup all hints from array if $q is not '' 
    if ($r !== '') {
        $r = strtolower($r);
        $len = strlen($r);
        foreach ($n as $name) {
            if (stristr($r, substr($name, 0, $len))) {
                if ($hint === '') {
                    $h = $name;
                } else {
                    $h .= ", $name";
                }
            }
        }
    }

    // In case there is no appropriate output or no input in the field
    echo $h === '' ? 'no suggestions :(' : $h;
?>