Class Checkpoint

Making Parkwood Vale · Part 11 of 13

Part 10

Add live book search: results appear under the search box as you type, using Ajax.

We make the search box in the header useful. As you type, JavaScript asks a PHP page for matching books and shows them without reloading the page. In this part: 0:01 What Ajax is 2:26 Adding test books and cover images 7:23 The results box under the search bar 11:21 search.php: finding books with LIKE 18:06 Handling no results 19:26 Showing each book with its cover, author and star rating 32:32 Adding jQuery and reacting to typing 40:53 Sending the search with XMLHttpRequest and showing the results 52:19 Font Awesome stars and final styling Software: Adobe Dreamweaver, UwAmp, HeidiSQL, PHP, MySQL, JavaScript, jQuery, Bootstrap, Font Awesome.
Transcript
  1. Okay, so in our previous sessions then, we've gone through self-registration and
  2. resetting our passwords. Last session, we went through how to update information
  3. within our database. So that principle, that creating a form in HTML,
  4. submitting the form to PHP, and then processing it as a select or an insert or an
  5. update command would be the same process for adding books to our database as well.
  6. I will go through adding books to the database in another video where we look at the
  7. API request from Google Books to get that information out.
  8. But for this video, I'm going to pre-populate the database with a few books and I'm
  9. going to take you through Ajax searching.
  10. Ajax searching is when we search our database without having to refresh our page.
  11. We use JavaScript, which is a J in Ajax, to post information to our
  12. PHP and retrieve that information, then update some form of HTML.
  13. So Ajax itself stands for asynchronous JavaScript and XML.
  14. Asynchronous meaning that it can happen at the same time as other things happening
  15. within our web page. JavaScript being the language is written in and XML is just
  16. a form of transport
  17. to allow us to pass information back and forth.
  18. Okay, let's get into it.
  19. Okay, so here I am back in my student account.
  20. And as you can see, I've opened UwAmp up.
  21. I've run UwAmp and that's it so far.
  22. I'm going to open up my website so I can see where we are with things.
  23. And what we're looking at today is this find a book section here.
  24. Now on the official Parkwood Vale site, if I just load that up a second,
  25. when I search for a book, so if I put in Harry, it finds any books that have Harry
  26. in place. If I just put in A, what it's actually doing is finding all the books that
  27. have an A in the title somewhere along the lines.
  28. So what we're going to be doing is a similar sort of process.
  29. When we search for something. A fly out appears and shows us anything that has been
  30. found in that setup there. At the moment, ours doesn't.
  31. So let's have a look at fixing that then, shall we?
  32. First thing we need to do is add some information to our database.
  33. Like I said in the preamble, I'm going to do this via database just to quickly add
  34. some in to work with rather than having to set up the whole page to start with.
  35. So I'm just launching HeidiSQL. I'm going to jump into my database and I'm going to go
  36. into my books section here. In my book section then, I'm going to go into the data
  37. side of things that will allow me to add in some records.
  38. So I'm going to go add for a second, ISBN.
  39. I'm just going to make something up.
  40. One, two, three, four, five, six.
  41. It doesn't have to be real. It's just so I can put something in place name Harry
  42. Potter author okay why did not take my name
  43. Harry Potter J K
  44. Rowling publisher who did publish Harry Potter let's have a look
  45. in here what have I got on this one do I not have any Harry Potter
  46. no
  47. okay let's have a look so Harry Potter publisher
  48. and it's Bloomsbury nice and simple
  49. then so in publisher then I'm gonna say Bloomsbury
  50. description I'm just gonna simply write
  51. Harry Potter book one I know it's not official
  52. but last one going with ratings i'm going to give it a four star image i'm going to
  53. leave copy i'm going to put three copies in place but it shouldn't make a difference
  54. to what we're doing in this section here then i'm going to go in and add in a second
  55. book just to have a look at this so it's a
  56. the isbn is the well i'd say the opposite but i didn't quite type that right one two
  57. three four five six there we go and six five four three two one and let's see what
  58. could we do as well as that
  59. Harry Potter books.
  60. What else have I got? My browse book.
  61. That browse book doesn't work.
  62. Let's go back to Parkwood Vale and browse books.
  63. See what we've got here. The Martian.
  64. Brilliant book. Let's go with that one.
  65. Martian. Now that will allow us as well to test this wild card a
  66. element here author I am being lazy I'm gonna copy and paste Andy weir
  67. and I'm going to see who the publisher is by going onto my page I did
  68. today I don't actually have the publisher Barnes education tested and
  69. let's see the Martian publisher
  70. okay let's bring that back and it was done by crowning
  71. crown a guy stuck on Mars
  72. it is the name of the planet after all let's capitalize that and I'm gonna give it I
  73. really enjoyed that book I'm gonna give it five and I just could put it two there
  74. but the jacket for the books are missing on Parkwood Vale if I type in a it
  75. does use the jacket so I am gonna need to have a jacket thankfully I can just steal
  76. this one save image as
  77. so webp already so i'm going to go into my documents i'm going to go
  78. into Parkwood Vale images now i haven't got a specific folder yet for the
  79. images the jacket images so i'm going to call it jacket there and just call it
  80. the martian actually i'm going to call it by the
  81. fake ISBN that I've given it. There we go.
  82. I'm going to go back into here.
  83. Let's jump back. I want Harry Potter book one.
  84. Images.
  85. Good old Bing images giving me prices instead.
  86. there we go save image as that's a jpeg i'm going to just give
  87. it my isbn my fake isbn and i'm going to need to normalize those pictures
  88. now in my jacket image element here i'm going to put in one two three four five
  89. six dot jpg for harry potter i'm going to do the opposite so six five four
  90. three two one dot WebP for the martian so i've got the jacket image names in
  91. place there's two books added into my database that i can use when we come to do
  92. the ajax side of things next step then is going to be opening up dreamweaver
  93. and getting my site back up to the way that it was.
  94. Did you just click discover? Yep.
  95. Happy with that. There we go. So here's my site.
  96. We have our header information.
  97. We have our account information.
  98. We don't need that, but we will need to add something to our header.
  99. If I scroll down on my nav bar and find my search element here, I have the find a
  100. book section, but I need to add something in place that will allow me to do a
  101. response. So once I've found a book and given it back to the
  102. JavaScript side of things, that something then shows me what that book looks like.
  103. What I'm going to do is just go in simply here.
  104. I'm going to add in a div and I'm going to call that div
  105. as id equals and call it live search.
  106. that will allow me to stylize that as I come through to it.
  107. Now, with the style of my live search, there are some properties I need to put in
  108. place. For example, this position, I want this position to be absolute,
  109. meaning that it's relative and it doesn't move around at all.
  110. It's linked to exactly where it is.
  111. I'm going to say that from the top of my page I want it to be 55 pixels
  112. down and then I'm gonna say that its background is made up so background
  113. itself there is background thank you very much
  114. is an RGB color which is and I'm just reading from my other screen here
  115. for what I use for Parkwood Vale, which is simply 239 each way.
  116. Now, what I'm using here is inline CSS.
  117. It allows me to add some CSS stylings to this section without having to sort
  118. of set it as a style elsewhere.
  119. It'll only apply to this div section here.
  120. Now, I'm going to tell it that the maximum height that I want this to actually be
  121. so it's controllable is 30 and I'm going to
  122. use the em function which is 30 or 20 then a size
  123. relevant to my page I'm going to say that it's overflow
  124. on the y-axis is set to scroll so
  125. if there are any more elements so it goes over the size of this 30 em
  126. it will automatically create the scroll bar functionality for me i don't want
  127. it to overflow the other direction so my overflow on my x-axis is going to be
  128. set to uh hidden so it's not seen in any sort of way.
  129. And the final property that's really important with this is that I'm going to set
  130. its Z index, which means what position it holds on the
  131. layer of my page is set to 100, so it's right the way in front of anything
  132. else that I include.
  133. Now, if I save that and I reload the page here, we'll see
  134. that actually nothing's appeared in because I haven't actually given it any content
  135. of sort yet. But when it comes through to it, and actually I didn't even put that
  136. up. So let's put that up first.
  137. There we go.
  138. And refresh that. And still nothing because there's nothing inside of it, but I have
  139. put it up. So it would have made that difference if needed to.
  140. Like I said, though, when I add some content into it, it will be bound by these
  141. style elements here.
  142. Now what I'm going to do next is create my search response
  143. element that will be triggering what happens within my finder book section here.
  144. And I'm going to do that by creating a new PHP page.
  145. So I'm going to right click here, go new file and that's going to be my new search
  146. PHP.
  147. So I'm going to say search.php.
  148. And open that up a second and let's clear out all of these elements.
  149. As always, I'm going to start with a PHP tag and I'm going to start with a session
  150. tag so that it knows that I am already logged in and it can use any information
  151. it needs to from my session to regard information.
  152. And I'm going to include the connection to my database.
  153. So I'm going to say config backslash connection
  154. dot PHP.
  155. Now that's all in place there.
  156. I'm going to end my PHP tag. I'm going to save this search page and I'm going to put
  157. that search page up.
  158. What I can do then is go forward slash search and I should get a
  159. blank page. I get no errors because I haven't asked it to do anything fancy yet.
  160. The next part is the complicated part.
  161. The next part is the interesting part where we set up our SQL to find what it is
  162. we're looking for.
  163. So I'm going to say my $SQL command is equal to, and it is going to be a select
  164. command.
  165. And I want to select all of the information.
  166. So I'm going to use a star symbol as a everything category from,
  167. and the page I want to select everything from is my books
  168. table sorry not page books table I then want to say where
  169. and I'm going to say books dot title so I'm talking about
  170. title and this is where it gets interesting I'm going to say like rather than using
  171. a direct representation I can use what's known as a wild card and by putting a
  172. percentage symbol around my information, what I can effectively do
  173. is say, I want it to sound something like this term I'm putting in place.
  174. Now I've got to wrap that in single quotes as well there, which does cause a little
  175. bit of a confusion for PHP. But there we go.
  176. Now that search term there, if I put A in place, that wildcard would select
  177. everything from books with the title contains an A at any point.
  178. And that's because I have a wild card before it and a wild card after it.
  179. If I only had the one wild card, that would look for any book that starts with an A.
  180. If I only had the starting, it'd be any book that ends with an A.
  181. By having the two wild cards, it's any book that contains the letter A.
  182. And that's all in place at the moment.
  183. I'm going to use my usual dollar sign result.
  184. $result equals $connection query
  185. and $sql
  186. to put that in place. I'm then going to print underscore R just very
  187. simply here a second to show you what's contained in result.
  188. and save that. Let's put that.
  189. And if I refresh this page,
  190. I get nothing.
  191. Shouldn't have got nothing. Let's see.
  192. What have I not done? I did definitely put that page two seconds ago.
  193. There we go. Let's go to my testing server.
  194. Search is definitely there and contains all of that information.
  195. That's exactly what I expected to see.
  196. There we go. Result equals connection.
  197. Very strange. Do you know how my books have, they do have A's in the title.
  198. Let's echo out simply dollar sign echo dollar,
  199. why did I do dollar sign? Simply echo dollar sign SQL.
  200. And let's put that up.
  201. When I refresh this, I get my query brilliantly.
  202. Let's copy that query. let's check it into my database
  203. and run it books title ah okay
  204. books books ddd what I call it name there we go
  205. from books with books dot name
  206. and save that let's put that up there
  207. refresh that and now we have some sort of response.
  208. So we don't longer need to echo that side out.
  209. Let's save that again and put that
  210. and there we go. So we now have two rows being given back to us
  211. and there are eight fields for each of those rows.
  212. So what I can do is set up my display to loop through each of
  213. those rows.
  214. so I'm going to put in a instead of a print here while and again I say dollar sign
  215. row equals
  216. result fetch associated rows fetch
  217. underscore ASOC
  218. and there we go and put that in place
  219. now that's going to loop through for each of my records and put out the information
  220. I wanted put out if i put that up there it will do nothing at the moment because i
  221. haven't actually printed anything but if i printed uh let's go echo dollar sign
  222. row and i want to just echo out simply the name
  223. save that put that up and
  224. refresh it it's not happy with line eight not realign eight name Why
  225. is it not happy with name?
  226. Okay, let's go.
  227. Select start from books like that.
  228. Let's take that out.
  229. And call to fetch_assoc. Why is it not happy with fetch_assoc for my results?
  230. Perhaps I need to check first of all, if I have any results to be found.
  231. So I've got my link there.
  232. to say if dollar sign result
  233. and I want to say num
  234. underscore rows doesn't equal zero
  235. actually if it does equal zero I'm just gonna do nothing
  236. so I'm gonna simply
  237. come here they break out of that PHP let's come back into PHP right
  238. here and that nothing has been found.
  239. So I say div class equals and I'm going to put this in a col
  240. md12.
  241. Inside that column what I'm going to do is simply print out a heading number
  242. four saying no results.
  243. So now I know that if I see no results that it hasn't found anything in place
  244. and refresh no results okay so
  245. that's giving me an error it didn't find anything like this where books that should
  246. be named not books that should now give me a result at least because we did expect
  247. to see a result and after that if I'm gonna say else
  248. and wrap this down here inside my house.
  249. Let's jump in a little bit because we're not going long lines today.
  250. We can actually see quite a bit there.
  251. So just to give you a couple of seconds to read that.
  252. Start the session, include our connection, create our SQL query, run our query,
  253. check if we have any results. If we don't, tell the user we don't.
  254. If we do, come into this section here where we're echoing out our information.
  255. That's where we're at. Let's put that up and see what happens over here.
  256. Okay, brilliant. We're back to the same issue, but now it's on line 16 instead of 8.
  257. So it doesn't like something along here.
  258. Why is it not happy with my name row then?
  259. Let's instead
  260. print underscore r and have a look what's inside row.
  261. So we should be able to now see what's going on with row.
  262. Let's just double check. I have got results, but right there I have.
  263. Brilliant. I got fetch_assoc. Doesn't need capitals.
  264. It's in its elements there. Perfect.
  265. Fine. And let's run it and see what happens.
  266. So in our associated array, we do have ISBN, name, author, and so on.
  267. Let's go back to echo and let's see if it's just being very pedantic.
  268. Row and name, because that's thing I didn't do was I didn't capitalize that
  269. in. Just run that and refresh.
  270. It's expecting an issue. I've forgotten my semicolon.
  271. Save it. Let's put it and refresh this page here.
  272. Happy days. It's not working. That's all I expected to see.
  273. Two different book titles.
  274. Now all we've got to do is populate how the books look.
  275. So when we have no information, we come back with no rows.
  276. When we
  277. of information to show them how it looks.
  278. I'm gonna break back out of PHP by here and just simply make sure I put in my PHP
  279. tag here to get back into PHP so that everything's in place.
  280. Now my echo there won't work that's perfectly fine I don't expect it to I'm gonna be
  281. putting in some base HTML with some CSS and it's just easier to come out of
  282. PHP for that. I'm gonna put a couple of div tags so I'm
  283. that is going to be my row tag.
  284. I'm going to put some padding around it so I'm going to put a ps tag in place three
  285. and I'm going to put some margin across the top of it.
  286. I'm also going to use three for that section there and that's my first div.
  287. Inside that div then I'm going to put in a column so div I'm going to put in class
  288. and it would be a column so col-md- and
  289. it's a three width column nice and simple that there is going to be where my image
  290. tag goes so I'm going to check image in a second and just do source
  291. equals and it's going to be let's
  292. see the base root we double check my naming images forward
  293. slash now in here let me just move get this in place you will see that I have the
  294. jacket there but if I go to my testing view in image I don't have jacket
  295. yet so I'm gonna have to put jacket up in a second but we'll deal with that in a
  296. second I'm gonna say jacket here forward slash and I want to adjoin to
  297. it my php element and I can do that just by checking a php tag in place here
  298. and echoing out echo should be dollar sign row
  299. and remembering I have to capitalize image
  300. that should echo out my image now if I save that for a
  301. second and I find my search my search up and
  302. I was to refresh this it's come up there telling me it can't find that picture and
  303. it actually says here the undefined index image in there
  304. let's just double check I'm not calling it the wrong thing by printing print
  305. underscore r again dollar sign row
  306. right and let's put that up refresh
  307. jacket image okay so I was calling it the wrong thing in here I want it as
  308. jacket image it's still not going to find that picture as I explained earlier I
  309. haven't put it in place it's still going to be an image error but that will be
  310. seconds time alongside my jacket image then i need to now put in place a
  311. div for the rest of my jacket details its class will
  312. be equal to a col md9
  313. giving me my sizing and this is where i'm putting in place all the
  314. other information about it so i can say a href because i want it to have a link
  315. on it is going to take me to my view book page which we haven't created yet don't
  316. worry about that and inside there i'm just going to say very quickly turn caps lock
  317. off if i confuse myself um full stop book and forwards
  318. uh dollar sign so i want to say it uh is
  319. bn is equal to and i'm going to echo dollar sign
  320. row single quotes isbn now i
  321. know book isbn page doesn't exist yet but we've got that in place so that when we
  322. have created it that should work if i click anything within this section then it
  323. will go through and do that i could wrap my picture in there as well but I'm not
  324. going to at this stage I may change my mind a little bit later on
  325. underneath that then inside the my link section here I'm gonna have a paragraph and
  326. in that paragraph I'm gonna have some details about my book so I'm gonna put in
  327. place a PHP type to echo out the dollar sign I want row
  328. in there and I want the name of the book
  329. and let me just save that and
  330. what exactly I included in Parkwood Vale's.
  331. So if I put in Harry, I got the name, I got the author and I got the star
  332. rating. So I got name there, I'm gonna do a similar concept.
  333. I'm gonna do that there and just simply put in author.
  334. Let's make sure I call it the right thing.
  335. It is just capital author. I'm gonna be even lazier and copy it from there and paste
  336. it there. And for now, I'm gonna do the same thing.
  337. I'm gonna do star rating.
  338. I think I just called that simply rating.
  339. There we go.
  340. Now, if I save that and I put that up and I refresh my page,
  341. I get JK Rowling, Harry Potter, four.
  342. Andy Weir, The Martian, five. What I expected and still have an issue with that
  343. picture, which is what I expected.
  344. To fix the picture with the images, what I'm going to do is click on my jacket
  345. folder here, press the up image,
  346. in is coming okay there we go all done now when i refresh this i get those images
  347. those jackets they're included that one's huge that one's not it is a good idea to
  348. try and resize and make sure all your images are the same in place so in my jacket
  349. here then let's have a look actually if i go inspect on that jacket because that's a
  350. decent size for it is 128 by 203 so in here image source
  351. d to d to d i didn't include name and image tag how bad of me there we go i'm going
  352. to say width
  353. equals and the image width here was 128 pixels so i say
  354. 128 pixels i say height
  355. equals and let's go back and double check a second that's 203 pixels 203
  356. pixels
  357. there we go so let's not put that up let's put this up and refresh
  358. this and they're both the same size perfect i don't need this array element this
  359. here at the top a second i can get rid of that so i can just remove my print row
  360. because i'm happy with that and that's all gone now my star rating this is
  361. a slightly different setup because i have just echoed the rating here but I want it
  362. to look a bit better than that.
  363. What I'm going to do is inside my PHP section here, then let's just drop this down
  364. and echo this out a little bit better off.
  365. And I'm going to say that my possible star rating, the total number, my stars,
  366. so dollar sign S is equal to five stars, top rating it could be.
  367. I'm going to say my star rating, my dollar sign SR is equal
  368. to my dollar sign rating, row sorry, and in my row it's getting
  369. me my rating element.
  370. Now I've got that in place there.
  371. I'm going to say for, check a loop in place, dollar sign
  372. iterator at zero,
  373. dollar sign iterator is less than dollar sign star rating.
  374. I want it to increase by one dollar sign I and I'm going to go plus plus.
  375. And what it's going to actually do in this section here is remove
  376. one from my star rating. So my S here.
  377. And it's going to echo out for me
  378. a span.
  379. Let's see how well this works.
  380. Class equals. Now, I know it's not going to show very much at the moment, actually.
  381. Can I do that? No, I can't do that, but I can do these.
  382. And we're going to have to include something else in a second, but we'll get to
  383. that. I use a font awesome fun awesome star
  384. and I've got that there
  385. now I'm gonna that
  386. closes there and let's close the span
  387. as well
  388. there okay that's in place now that's echoing out my stars
  389. and I'm gonna add in a class called checked which will hopefully when we get around
  390. to doing it or when we've programmed it in change those to gold.
  391. I'm going to repeat this then this whole for loop for the remainder
  392. of my stars. So if it's a four star rating it will span out four
  393. checked stars.
  394. Otherwise it will span out four whatever remained at normal stars and put
  395. that in place.
  396. now if I save this and I put this up I know it's not going to do very much at the
  397. moment because I haven't got that font awesome in place when I refresh it you can't
  398. see anything there
  399. that's perfectly fine because we're going to include font awesome on our main page
  400. which will allow us to see our star rating I wouldn't expect to see it by here but
  401. that span class is in place for my star rating my row rating if I was to go
  402. back and I was to inspect
  403. And look at this. Inside my div, I have my image, which is giving me an error there.
  404. And I'm going to look why image must have alternate text.
  405. I haven't done that. I'm going to go down here.
  406. It's giving me my clickable stuff.
  407. Inside my clickable stuff, I got my paragraph.
  408. I got my Andy Weir as the title.
  409. And then I've got the five star checked elements.
  410. That can't be seen because I haven't included anything to show them.
  411. We do the same thing inside Harry Potter.
  412. A second then. Expand out these properties.
  413. you will see that there are four checked and one non-checked.
  414. Again, not being shown because we haven't included Font Awesome in this section, but
  415. they are all there to be seen.
  416. So that is the layout of what I want to represent in
  417. this section here when it drops down.
  418. But obviously we don't have that dropdown in place just yet.
  419. So that's our next step then, is how do we get that information to be requested and
  420. put in place here? We've got our search there, and at the moment it's using always
  421. searching for books with A in their name.
  422. We want to replace that. We're going to replace that with some information as we go
  423. forward. But what information do we replace it with and how do we do it?
  424. That is where our Ajax element comes into place.
  425. So our Ajax element is going to be available on
  426. all of the pages that our nav bar exists on.
  427. So we need that in our main footer for our main document.
  428. So I'm going to go into my footer page here where I have the footer content and then
  429. my JavaScript element. And that's where I'm going to be putting in my Ajax searching
  430. elements. So I'm going to put in a script tag here
  431. that will allow me to script my JavaScript.
  432. The type is going to be of text and then it is going to be a text
  433. JavaScript and I'm just going to simply finish that off.
  434. So there is that element there and what I'm going to be doing is using a mixture of
  435. JavaScript and jQuery which if I'm looking directly here I'm not sure if I've
  436. included jQuery so I may need to include jQuery as well and
  437. use that to search through these facilities.
  438. Now jQuery I just mentioned a couple of times is a JavaScript library and
  439. if I have a look very briefly
  440. That should be okay there. So what I'm going to do is search for
  441. jQuery CDN. Now,
  442. CDN is a content delivery network that allows you to effectively
  443. grab jQuery and use it within your website without having to download and install
  444. and put everything in place. So I'm going to go for the minified version here.
  445. I'm just going to copy my jQuery tag there and I'm just going to put
  446. that because it likes to be first above these sections here If I
  447. save it and just put my footer up for a second Just to make sure that hasn't messed
  448. with the way that my website works as is I
  449. Still get the fly out jQuery hasn't upset that and I'm going to continue and use the
  450. jQuery elements within my section here
  451. now what jQuery allows me to do is target different elements so I can use the simple
  452. dollar sign expression open a bracket and then say that I want to use the
  453. ID so the hashtag of my search which should be
  454. from my header section the ID given to my search bar and I'm going to say that
  455. when the my search has an input so I'm going to say on and I'm going to say
  456. input
  457. Then I wanted to do something and what I actually wanted to do is run a function for
  458. me the function is going to be requesting and finding that information out
  459. So I can say that there Let's just bring this down and end that there and it is an
  460. empty function So not passing any parameters to it and to start with what I'm gonna
  461. do is get to alert
  462. So I can check that it's working and the alert is simply going to say
  463. now if i save that and let's put that footer page up and
  464. refresh here if i type in here i get an alert that says hello and every time i
  465. type it's giving me an alert doesn't matter what it is i type it just gives me that
  466. alert i'm pretty happy with that that's exactly what i expected.
  467. What I want it to do though is I want it to tell me
  468. what it is I typed.
  469. So I'm going to say that my value comes from
  470. the dollar sign this expression so whatever's posting it dot val.
  471. now if i save that and i actually need to wrap that as a function so save don't
  472. duplicate save and put that footer up refresh my page
  473. now if i put in g it says g if i press delete it says
  474. nothing if i put in h e l
  475. l o very slowly i spell the word hello and when i delete it
  476. So we don't want it to fire and try and search every time it does something because
  477. if there's nothing in there, then we don't want it to do anything.
  478. What we can do is compound this with a length check and see what the length
  479. of our input is.
  480. So if I go up to here, refresh my page now, and I put in G,
  481. it's not happy with my length function because length isn't a function.
  482. It is just a property. There we go.
  483. Save that. Let's put that up.
  484. and refresh.
  485. Open in G, it says one. If I say G, two, every time I add a letter, it is increasing
  486. the length of the input on this box.
  487. And if I go like that, it says zero.
  488. So what we can do is say, instead of alert, I'm going to say, if the
  489. length input is equal to zero, then
  490. we're okay. Don't do anything at the moment.
  491. If it's not equal to zero and say else that's when I wanted to do something
  492. and we're going to bring back that alert in place just very quickly and use the
  493. value again just so that we can show our alert is working.
  494. Save it and let's post it.
  495. Refresh. Perfect. So if I put in H E
  496. L L O
  497. we get hello. If I delete it we don't get anything.
  498. If I put in R and then I delete the letter R we don't get anything because
  499. it's falling into this section here and it isn't doing anything.
  500. Just perfectly fine that's exactly what we wanted to do.
  501. in fact we're going to go a step further and we're going to prepare ready for what
  502. we're going to do because we're going to say that our document which is the current
  503. working document that we're working on i want to get element by id and as you can
  504. see it helps us and it already puts that in place similar to our javascript we did
  505. with our sliding image gallery we use class name there this time we're using id the
  506. id that we're going to be looking for comes from our header which is my
  507. I say live search there and I'm going to say that the live search dot inner
  508. HTML
  509. is equal to nothing that clears it all out and gets rid of it
  510. and I'm simply going to return to say nothing on this response
  511. I'm going to say document dot live search inner HTML equals And I'm going to say my
  512. value. So whatever it is I've specified and put in place
  513. Let's save that and put that up instead We don't need the alert Really let that
  514. confuse things that would break things.
  515. Let's get rid of that save that and put that up
  516. And refresh so in here now if I put in H E LL O
  517. you can see that the content
  518. I'm typing appears in the
  519. box below.
  520. And it actually has that set restriction, so it can't go over the size of that
  521. space. And if I delete it all, the box disappears.
  522. So that is working as our basic element of searching, updating our page,
  523. using those AJAX functions, but not actually changing anything.
  524. We now need to focus on this section here and we want to change it so that it brings
  525. back the information from our search page and displays it in that section.
  526. And the way that I'm going to do that is to set up a new element called
  527. xmlhttp equals new and surprisingly it's going to be an
  528. xml http request and that is going to set up my
  529. request that i'm going to send to it my xml http is going to be an on
  530. ready state change function
  531. and that is going to be a new function called and again it's going to do nothing now
  532. i know we've got quite a few functions on the go here doing different bits and
  533. pieces they are all needed to work as we expect it to what that's
  534. going to say is have a request to my server and on ready state
  535. change then execute this rather than just going off and getting it
  536. now inside that i'm going to say if this as in my xml
  537. ready state and i'm going to say ready
  538. state
  539. equals
  540. 4
  541. And I'm going to compound that with a this dot
  542. Status
  543. Because I want to check the status code coming back out is
  544. Equal to 200 which is an okay.
  545. Everything is fine status. Then I wanted to set my document value
  546. 2 and let's tab that in
  547. out and it is going to be instead of this value it'd be this as in my
  548. request response
  549. and it's going to be the response text so whatever they get from this request when
  550. it's ready if it's a valid request then give us that response in this
  551. if element
  552. let's bring that back one line
  553. and if did it just making sure all my tie-ins tie up nicely
  554. So the XML request, the HTTP request that I'm going to put in place
  555. is going to open a connection.
  556. And the connection is going to be a style of get because I'm going to use a get
  557. string like we did previously.
  558. It is going to be going to my search page.
  559. And it is the PHP page. Now, I know that we don't necessarily use .php with the rest
  560. of our file names, but we do need to include it here.
  561. I'm going to say $. My query, I'm going to simply put down to Q.
  562. And then in there, I'm going to say plus $ this.
  563. And this should be in brackets.
  564. There we go. Dot val.
  565. Now, the last parameter within this open element here is just simply going to stay
  566. true.
  567. So that's going to send that request.
  568. It's going to call this request here,
  569. recall it, and once it's ready, give me the response it's got.
  570. The last part is going to be the XML HTTP request.send.
  571. So now what's going to happen is if I enter something and it's longer than
  572. nothing, it's going to set up a request.
  573. It knows what to do when the state has changed to 4, which means it's got a
  574. response, and the status is 200.
  575. my live search with the response it gets from this search.
  576. Now this search is passing a parameter through which we haven't defined on our other
  577. site yet, but it's all set up and we have our jQuery in place.
  578. If I save this and I put my footer back up, what I should be able to do
  579. is refresh my page because it does need to update the JavaScript at the bottom.
  580. And if I put something in this box here,
  581. should search and respond to me.
  582. But for some reason, I'm not getting anything.
  583. Let's have a look at my inspect.
  584. I do have errors in inspect and it says, failed, use the new operator.
  585. Okay.
  586. So where did I forget to put the new?
  587. Here.
  588. And now that we've got the new operator in place, let's put that up.
  589. Refresh this. I'm gonna keep this open just in case I've made another mistake.
  590. And if I put in the details, look at that.
  591. back. If I get rid of it, it comes back.
  592. It's fine in all books at the moment because I haven't told it to do anything
  593. different.
  594. In here, if I go back to my search page, remembering I called q in my get string,
  595. I can change this to say
  596. $sign underscore get.
  597. yeah just making sure i didn't mess that up there we go and i
  598. simply want to say q for my query now if i put search up
  599. the brilliant thing with this is i don't have to refresh my page anymore
  600. because it's going to that search page but it's going asynchronously So
  601. if I put in a it will find them and it's also giving me an error which is not happy
  602. with q and an undefined index element there.
  603. Let me go back in. Let's see.
  604. Get q there in my footer. I'm saying search.php.
  605. q is in my get string.
  606. Search. q. Save.
  607. But.
  608. Okay
  609. That all looks pretty alright
  610. Why does it not see
  611. Undefined index Q
  612. Sources let's go on to This page at the bottom here.
  613. This is just a back end of it search equals Q
  614. Okay, let's do search
  615. I'm gonna say Q equals A.
  616. If I search without Q, it's not happy there.
  617. If I do search.php, Q equals A,
  618. should be working fine because if I put that in there
  619. as the Martian has an N but Harry Potter doesn't, it only brings back Harry
  620. Potter.
  621. um perhaps it shouldn't make an issue
  622. right let's have a look at the javascript on here and
  623. see what it's doing inspect so sources and
  624. i want to come down to here so what i've done here is i've used the
  625. sources section of the inspect and I put a breakpoint there so when I hit that
  626. breakpoint it should pause as it has and I can see what information is getting
  627. there this val is there
  628. and possibly give me an error on that section there because q hasn't been given that
  629. right element let's come back into here and say dollar sign this
  630. should be working fine ah
  631. val is a function val does need a function let's put the footer in place and pop
  632. that up
  633. and i can get rid of that breakpoint i can get rid of the terminal element because i
  634. am very confident this is going to work but this time i do need to refresh because
  635. i've changed the JavaScript at the bottom of my page.
  636. I put it in N.
  637. I shouldn't have been so confident.
  638. Why didn't that work?
  639. Let's footer save Q
  640. equals. I've got the equal symbol.
  641. Put the footer up and refresh.
  642. N. We just get the Martian in place.
  643. We no longer have that additional information.
  644. We only have what we want.
  645. Now I am in a situation where my book has been displayed and the title is there.
  646. It will take me to where I expect it to take me to, but if I put in
  647. A there, if I put in N, it gets rid of Harry Potter.
  648. They are working, but the layout isn't quite what I was expecting.
  649. Not quite as nice as it is here.
  650. Now let's have a quick look. So we've got our search page here
  651. then.
  652. We have our row. We have our elements for our row.
  653. Let's see. So div class row. Div class md3.
  654. We've got a size.
  655. I am also setting the size quite harshly there, which if
  656. I go back,
  657. in n there, that book is quite big, if I put in n here, the jackets are a lot
  658. smaller. What size is that jacket there?
  659. What did I use on this version?
  660. That is 48 by 74. So let's say instead
  661. we do 48 by 74 pixels. Save
  662. that. Let's find search and push search back up and
  663. and that is much more in line with what I was expecting to see
  664. the Martian and you we're not going down the second line I wanted to go down on a
  665. second line so I simply put in a break
  666. and let's push that up and again because it's Ajax
  667. I can just do that now it's gone over to that side there let's actually remove our
  668. width section for our picture and see how that behaves because it should be governed
  669. by our columns here and our different elements in place so
  670. let's put that and let's go and
  671. nope it's not liking that let's check those back in for a second then
  672. save and let's put that up.
  673. Let's do call eight and margin left two and
  674. put that up and see how that looks.
  675. It's a little bit tight, a little bit squashed.
  676. I'll leave with that for a second, leaving that there.
  677. I'm not going to overly fuss with that.
  678. What I did say was that this isn't necessarily showing its rating because it
  679. doesn't have in place the elements needed for our star rating and one of them being
  680. font awesome f a star on my header page then on my header section here
  681. where i include a lot of css elements i bootstrap on those different bits and pieces
  682. i want to use and again a cdn for font awesome
  683. now font awesome comes in a variety of different
  684. awesome 4 is free and open for most people to use so font awesome 4
  685. cdn if i go into here there is the font awesome css and actually that's using
  686. version 6. as i've clicked onto it it's copied it so i can just come over to here
  687. paste it
  688. is a type of CSS link
  689. and rel equals
  690. stylesheet there you go so save that let's check
  691. header up
  692. and refresh my page.
  693. Refresh my page. Now, let's see if it's worked.
  694. And we're getting somewhere. We have empty boxes.
  695. Now, I use Font Awesome 4. I wouldn't necessarily use Font Awesome 6
  696. in this regard because it can cause issues.
  697. So let's go back to Font Awesome, CDN Libraries.
  698. I do want font awesome forks. I know that had my stars in
  699. it and it was 4.7 I used and here we go 4
  700. .7 CSS.
  701. That's the one I want to copy URL.
  702. So let's put in place the version I know included my
  703. stars up that and refresh my page a
  704. couple of times but in the end we have stars our stars are in place they are links
  705. so let's go back into our search box for a second and again we want to
  706. break row so we get on to the next line and let's put those up I don't necessarily
  707. want the star rating to be that color for my a so i'm going to end my a tag
  708. there and that will also cause issues my p tag so
  709. let's go paragraph there and save
  710. and let's see how that looks now again because it's ajx i can just delete it
  711. we get this here there's our stars the only thing we don't have is that checked
  712. value where I want to set it so that it changes color to gold if it is
  713. checked.
  714. Now, what I can do is simply put into the normal header section up here,
  715. and I'm going to go as a style tag,
  716. and that I'm going to say dot checked,
  717. which was the style I used in my search element here.
  718. so if they are checked they come there and it's a color
  719. is gold
  720. go golden rod looks quite nice color actually and save that in my header
  721. section header put that up and
  722. hey hmm
  723. inspect element why did it not like to change the color
  724. of my star checked
  725. okay let's go color
  726. golden rod yes that's
  727. be like perhaps it didn't pull through and update that so let's check this in a few
  728. times there we go head let's check there's my style check
  729. there we go okay and then we have it they are now coming through correctly with
  730. this star rating colored in golden like I've got on Parkwood Vale so I've managed
  731. to put all that in place the only thing I'm having issues with at the moment is my
  732. padding and the layout elements I've got there.
  733. And let's go search. I've got this in place here.
  734. Padding, D2D2D.
  735. Let's check this back to nine.
  736. That's got that there. Save that.
  737. It's going to come through. It's going to put that in place.
  738. Where can I try to add some better padding?
  739. That's on my search. Let's check that up.
  740. and put in a
  741. okay so inspect let's have a look at this and we're going to
  742. bring this over to here our picture there image okay
  743. let's say margin
  744. and i want say two pixels
  745. that's not having any sort of impact at all that's not what we want to do we don't
  746. want to put a margin on our image there that hasn't made a blind bit of difference
  747. why is our picture what if we set instead attribute
  748. width to be 100% of the box that
  749. up and we get rid of the height element restriction altogether
  750. you look at that so that's fixed that exactly
  751. how I want to do so let's go back here width is gonna be 100% of its parent its
  752. height is going to be relative to its width and I'm gonna save that I'm gonna put my
  753. search up if I close that I will this page this time is to clear the CSS out that I
  754. edited on the side there and if I put in a we now have that in place and we have
  755. those two books and I'm going to check in a few more books up here a second I'm
  756. gonna go to data let's copy I'm gonna duplicate rows with keys
  757. and all I'm gonna do is add another thing there just so that we can say another book
  758. so I'm gonna say not the not Martian and I'm going to do something similar with
  759. Harry Potter.
  760. I'm just duplicating keys with rows.
  761. There we go. S and Harry Potter 1.
  762. Okay. Now, if I go back into here and I delete that and I put in A again, we get all
  763. of those boxes appearing.
  764. Now, what height did I specify with my maximum height in this section here.
  765. Let's see.
  766. No, it was in the header in this section here.
  767. I said that my position relative maximum height was 30 EM.
  768. If I used to change that down to 10 EM and save that and put my header up.
  769. Refresh.
  770. We then have our scroll bar because it's taken up less space.
  771. So that has allowed us in this section to put in that flyout window,
  772. use JavaScript to search our database and find it using AJAX and pull it all
  773. together.
  774. Hopefully that all makes sense and now I'm staring at it.
  775. Hopefully I record the right screen as well.
  776. Let's see how that goes.
  777. Thankfully I did record the right screen otherwise that would have been a very
  778. painful hour to go back and do the whole thing again.
  779. Thank you again for watching. If you have any questions any issues or anything like
  780. that please don't hesitate to get in contact and I will do my best to support you.
  781. Thank you.