Thursday, March 18, 2010

Use JQuery to make an AutoSuggest box in PHP - a Tutorial

At times it's easiest to wire up your own ajax page with a sprinkle of XMLHttpRequest() here and .readystatechage there. Other times, it's pretty handy to use a well packaged library, like jQuery, where most of the heavy lifting has been done for you.

Let's say you have two simple pages

search.html









which, when you type in some text into "namesearch" and click "search" will POST to the page

results.php



<?php
$name = $_POST["name"];
$con = mysql_connect("localhost","root","");
if (!$con){ die('Could not connect: ' . mysql_error());}
mysql_select_db("friendface", $con);
$result = mysql_query("SELECT * FROM users where name like" . " '%" . $name . "%'");
//output the results to an unordered list
echo "
    ";
    while($row = mysql_fetch_array($result)) {
    echo "
  • ".$row['name']."
  • ";
    }
    echo "
";
mysql_close($con);
?>



so, when you type something into the search field



and click submit you see a list



Now, if you want to have the results show up below the search bar, just drop in a reference to jQuery in the header.


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>


Now, let's fire a function in the "onkeyup" event from the search box:





which will call a javascript funtion "suggest" and pass it the value of the textbox. Now, we have to let the page know what that function should do. What we want it to do is to call the results page, get the results, and display them on the page. All using Ajax.

So, let's make a <div> area for the results to be displayed. place the following after the button





which gives the page a div to drop some results into, and back up in the header, let's make out js function.




so by this point, your whole page should look like this

search.html



Now, when you type int he text box you should get the results showing up just below the search field.

Of course, it would be nice to pretty it up a bit. Add a line in the head section

<link type="text/css" href="style.css" rel="stylesheet" />


create a new file

style.css



#namesearch{
padding:3px;
border:1px #CCC solid;
font-size:17px;
}
#suggestionList {
margin: 0px;
padding: 0px;
height:20px;
width: 200px;
font-size:16px;
font-family:Arial, Helvetica, sans-serif;
color:#333;
padding:5px;
margin-bottom:10px;
}
#suggestionList ul li {
list-style:none;
margin: 0px;
padding: 6px;
border-bottom:1px dotted #666;
cursor: pointer;
}
#suggestionList ul li:hover {
background-color: #FC3;
color:#000;
}
ul {
font-family:Arial, Helvetica, sans-serif;
font-size:11px;
background-color:#000;
color:#FFF;
padding:0;
margin:0;
}


and reload your page. Lovely.

Tuesday, February 2, 2010

Better World Books

Do they really have to be so awesome? Here is the shipping confirmation page:
---------------------------------------------------------------------------------

Lloyd Cledwyn
Your order has been shipped!
Better World Books Tue, Feb 2, 2010 at 9:23 AM

Hello Lloyd,

(Your book(s) asked to write you a personal note - it seemed unusual, but who are we to say no?)

Holy canasta! It's me... it's me! I can't believe it is actually me! You could have picked any of over 2 million books but you picked me! I've got to get packed! How is the weather where you live? Will I need a dust jacket? I can't believe I'm leaving Mishawaka, Indiana already - the friendly people, the Hummer plant, the Linebacker Lounge - so many memories. I don't have much time to say goodbye to everyone, but it's time to see the world!

I can't wait to meet you! You sound like such a well read person. Although, I have to say, it sure has taken you a while! I don't mean to sound ungrateful, but how would you like to spend five months sandwiched between Jane Eyre (drama queen) and Fundamentals of Thermodynamics (pyromaniac)? At least Jane was an upgrade from that stupid book on brewing beer. How many times did the ol' brewmaster have one too many and topple off our shelf at 2am?

I know the trip to meet you will be long and fraught with peril, but after the close calls I've had, I'm ready for anything (besides, some of my best friends are suspense novels). Just five months ago, I thought I was a goner. My owner was moving and couldn't take me with her. I was sure I was landfill bait until I ended up in a Better World Books book drive bin. Thanks to your socially conscious book shopping, I've found a new home. Even better, your book buying dollars are helping kids read from Brazil to Botswana.

But hey, enough about me, I've been asked to brief you on a few things:

We sent your order to the following address:

Lloyd Cledwyn
***************
SAINT PAUL, MN 55105
USA

Order #: 10****


We provide quick shipping service to all our customers. You chose USPS Standard Mail shipping, your book should arrive within 4 - 14 business days. The Postal Service may occasionally take slightly longer to deliver your book.

At this time, we are not able to offer tracking on our USPS Standard Mail shipments.

If you have any questions or concerns, please email my friends in Customer Care at help@betterworldbooks.com. If you could please include your order number (10867312) that would be very helpful.

Eagerly awaiting our meeting,

Walt Disney's Peter Pan (Illustrated Classic)
Peter Pan in Kensington Gardens and Peter and Wendy (World's Classics)

Thursday, November 5, 2009

Awesome jQuery Tables - DataTables

Short asside: I've been mulling over this project of having a blog of things that I believe are of quality. That add to the quality of things in the world. This post is in that spirit.

When I first saw Ext-js I thought. "Awesome!!!" then I dug into implementing it. Right. building data structures into xml trees to pass to a javascript aware element on your page. Bam, right off the top of my list of fun things to work on. So, much to my joy did I discover the DataTables project. http://www.datatables.net/ Which extends the already awesome(and nearly painless to use) jQuery to make an html table come alive with some nice jQuery stylings.

Seriously. Take their "No Config" example and look at the code behind for some sweetness.

You take a page like


Then you drop in a few lines in the section. Drop in jQuery and the dataTables extension, then activate the table. (well, obvisouly you have the js files referenced)
<script type="text/javascript" language="javascript" src="/js/jquery.js"></script>
<script type="text/javascript" language="javascript" src="js/jquery.js"></script>
<script type="text/javascript" language="javascript" src="js/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8">
  $(document).ready(function() {
     $('#example').dataTable();} );
</script>
and right away you have pagination with counts, and and an Ajax search.


Sweet!! Thank you Allan Jardine.

Wednesday, November 4, 2009

Day 3


Boo
Originally uploaded by lloydcledwyn
International Lloyd Does Something every day even if it's no so amaizing month day event thing.

Monday, November 2, 2009

Day 2 - More clay


2009-11-01 014
2009-11-01 012
Originally uploaded by lloydcledwyn
Working with this form/technique for a while. Totally stolen from you. Apparently. Because I have had enough potters tell me that I must have stolen this form from them. So why not you too.

Aside from that, it's an easy shape for a hack-artist like me to play with the idea of proportions. Inner shape, outer shape. Etc.

Oh yeah, and a doggie. Cuz, why not.

Sunday, November 1, 2009

Earthenware plate


Earthenware plate
Originally uploaded by lloydcledwyn
Day one of my skew of NaBloPoMo. Make Something Month.

Some bread


Some bread
Originally uploaded by lloydcledwyn
Day one of my skew of NaBloPoMo. Make Something Month.