phppup Posted February 11, 2018 Author Share Posted February 11, 2018 So AJAX just makes the effort more dynamic? What an I searching for to learn and find what I need? Quote Link to comment Share on other sites More sharing options...
Barand Posted February 11, 2018 Share Posted February 11, 2018 You could set it up with a simple form with a name input field. In the javascript, whenever a key is pressed the current content of the name field is sent in an ajax request. On receipt of the request, the target php script queries the database for names beginning with enter letters and sends back the list. The requesting javascript waits for that response and outputs the list to the page. This is repeated each time a key is pressed in the input field. The list gets smaller each time as less records match the input. Using JQuery in the javascript severly reduces the effort in making ajax calls. I have knocked together a small script to do that. (It currently uses the actor table in MySql demo database "sakila" but can easily be adapted to your own if you change the db name in the connection and the table and field names in the query.) Disclaimer: I am in the middle of rebuilding my development environment of a new pc (only got it yesterday and php and my IDE not yet installed) so this script is untested, but it should show you the elements and how they fit together. Hope it helps. pup_demo.php <?php error_reporting(-1); $username = '****'; $password = '****'; $host = '127.0.0.1'; $database = 'sakila'; $dsn = "mysql:dbname=$database; host=$host; charset=utf8"; $db = new pdo($dsn, $username,$password, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ]); #*****************************************************************************# # THIS BIT HANDLES THE AJAX REQUEST # #*****************************************************************************# if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { $name = isset($_GET['name'] ? trim($_GET['name']) . '%' : '%'; $stmt = $db->prepare("SELECT actor_id , first_name , last_name FROM actor WHERE first_name LIKE ? OR last_name LIKE ? ORDER BY first_name, last_name"); $stmt->execute( [ $name, $name ] ); if ($stmt->rowCount() == 0) exit("No matching names found"); $out = ''; foreach ($stmt as $cust) { $out .= "<label><input type='radio' name='customer' value='{$cust['actor_id']}' > {$cust['first_name']} {$cust['last_name']}</label><br>"; } exit($out); // output the list of found names } #*****************************************************************************# # END OF AJAX REQUEST # #*****************************************************************************# ?> <html> <head> <meta charset="utf-8"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script> $().ready( function() { $("#name").keyup(function() { var partial = $(this).val(); //********************************************** // MAKE THE AJAX REQUEST //********************************************** $.get ( "pup_demo.php", { "name": partial}, function (data) { // output from the ajax handler will be in "data" $("#namelist").html(data); }, "TEXT" ) }) }) </script> <style type="text/css"> #search { width: 400px; float: left; } #namelist { width: 800px; float: left; margin-left: 25px; overflow: auto; } </style> </head> <body> <form action='pup_update.php' method='post'> <fieldset id='search'> <legend>Search for</legend> Enter partial first or last name</br> <input type='text' name='name' id='name' value=''> </fieldset> <div id='namelist'></div> <div style='clear:both'></div> <input type='submit' value='Record customer visit'> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
phppup Posted February 12, 2018 Author Share Posted February 12, 2018 Thanks a ton. I have already begun doing some homework regarding AJAX and quickly learned that it sometimes (as I'm my case, naturally.. LOL) fails when used from smartphones. I will test your script and the others I've uncovered in tutorials tomorrow. Quote Link to comment Share on other sites More sharing options...
dalecosp Posted February 13, 2018 Share Posted February 13, 2018 Thanks a ton. I have already begun doing some homework regarding AJAX and quickly learned that it sometimes (as I'm my case, naturally.. LOL) fails when used from smartphones. I will test your script and the others I've uncovered in tutorials tomorrow. Smartphone? You said: At this point I am trying to track physical visits to a physical store with actual communication between a live employee (at a cash register) and a local customer base. Here are some assumptions we have to make based firstly on your input, and secondly because this forum is about PHP: 1. You will have server software running on a machine with a network interface. 2. You will have database software running on that machine or on another machine on the same network. 3. You will have a web-based client running as your GUI on the machine used by the employee, which serves the business as its "cash register". If these things are not true, you're at the wrong forum. Furthermore, unless PHP is running on the server in #1, you're probably not at the right forum either. AJAX is being suggested in order to avoid re-load of a page on the client (cash register). Truth be told, a cash-register app in a web browser might be a bit of a programming challenge, much less the back-end database of customers. But it's not insurmountable. Can you show us your database schema? As some have noted, you need a unique identifier, and I would concur in suggesting that you use the customer's phone number, which someone else (namely the phone company) has already worked on to ensure uniqueness. If there is a customer perception issue surrounding this, you should suggest a statement to allay such fear, perhaps something like, "we collect your phone number to notify you if there are safety recalls on your product and provide a record of purchases in the event you must return your item for service or a refund." Hope this helps. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.