[ Silence ] All right. Good evening everyone. My name Alain Ibrahim and tonight I am presenting a section on AJAX and walkthrough for project 2. The first thing I would like to ask you is what is AJAX? After David's lecture, can anyone define what AJAX is or tell me succinctly what it would mean when we say AJAX? OK. AJAX, I will tell you what AJAX stands for. AJAX stands for Asynchronous JavaScript and XML. I'm not going to go into the why it mentions XML now, but at the time of inception, XML was mostly popular as opposed to JSON as a format of data interchange and so forth. The main-- the crux of AJAX is actually JavaScript. Let me move on to the next slide. So, here we go. All right. So, AJAX is really a combination of existing technologies. One being HTML, although HTML is not the functional component of AJAX, it is the representation layer. So essentially, if you have your JavaScript, XML or JSON and so forth and you are making a call of some sort, it's kind of maybe useless if you're not going to present it in some visual fashion, hence, the need for-- hence the presence of HTML, in my definition of AJAX. You do need JavaScript because AJAX relies on an object, a JavaScript, say, a class called XMLHttpRequest. And this object acts as a liaison between the client side and the server side passing data back and forth in the background. Before AJAX, we used to have pages like client browsers, make requests to the server side, and what would essentially happen is the server would output some text or some HTML and the entire page on the client side would be reloaded with the-- with AJAX, with the inception of AJAX. What happens now is the requests are sent in the background and they are responded also in the background while the remainder of the client side script is still working. And once the client side script receives the response from the server, it can then interpret it and apply it accordingly to the document, object model, or it can do something else with it for example. In the acronym of AJAX, there is the asynchronous word and I will stress a little more on that, what it means. AJAX is asynchronous in the fact that when a request is sent to the server, the remainder your client side quote, your JavaScript quote can be executed and/or-- and that's stopped before the server has responded back to you. So when-- let's suppose you have a function that calls, asks for, like in David's example, asks for a quote and your webpage does other things other than show you the quote, your client side page will do the other things and the servers-- meanwhile the servers working on the background over the internet, and once it has responded, your page can then take that response, interpret it. Now, AJAX is not always asynchronous, AJAX can also be synchronous. There's a little setting you can toggle in your function, and that's you can make AJAX synchronous such that it will not continue executing the rest of your JavaScript quote until it has received a response back from the server. And I will show you this briefly. OK. I'm not going there yet. So now, I will demonstrate an example, some source codes where I dive a little deeper into the inner workings of AJAX. OK. So what I have done here on this side is I created two files, just two files, I do understand that David shows you different iterations of the examples, and so forth as they build up, but I'm going to simplify it and the actual different features are all embedded in both of these files. So there's a-- I have an ajax.html page and an ajax.php page. The ajax.html page corresponds to the client sides of AJAX, and the ajax.php page is the actual server side script that the-- that is being called from the client side. OK. I will first open up ajax.html. It is a typical HTML document, HTML file document as specified here. And the head tag I make a call to receive the jQuery library from Google. So this first script tag references the jQuery library from Google, this has nothing to do with AJAX right now, but I will be using it later to apply the methods like that section as dot AJAX that David had mentioned in class. And let's see what else. The next set of script tag is will define what I created for this example. OK. So for a complete AJAX turnaround, what do we really need to do? There are, in my opinion, there are three main things that need to be done. Underneath the hood, you need to first create an XMLHttpRequest object. That is the first step. The second step is to utilize this XMLHttpRequest object to send data to the server and receive data back from the server which takes us to step number three, which is the response function. There will be a callback function that takes the data that is being sent by the server back to the client and handles-- and that handles the data accordingly, according to your code or you design decisions. Now moving to the code, I-- the first step here for me, first step that I have done is I have created a variable called xhr, which will host or which will be the placeholder for the XMLHttpRequest object. I have set it to null and I have defined this specifically outside of the functions so that all the functions can have access to it once we have created the new XMLHttpRequest object. And the second step is that I created a function called createHttpRequest that would return an actual XMLHttpRequest object. David had mentioned this in class so I will not repeat-- or I will not dive into this again. But basically, the basic idea here is that you are creating an XMLHttpRequest object through this function, and that's what-- that is what this function is returning. And now I will-- I created my own function called getText which will just basically get text from the server side, which it-- or in this case, ajax.php. So in this function, I defined a few things. First thing I will define is a variable called params, where I insert-- where I defined what parameters I will send to the server along with their values. In this case, I-- I'm defining a variable called i_want_text and I have set the value of this to yes. This is really nothing meaningful, but it's just an example. This works also in the same way that you would normally do in a-- when you send request via URL and you put the ampersand sign. So if I want to send more than one variable here, I would put ampersand and say variable 2 equals to 5 for example, and when send this-- and this will allow me to send the variables over to the server, I mean not this alone, but this is the structure of how they are laid out. Next is I'll define a variable called url which points to the server side script, in this case, it is called ajax.php. It could have been something like http:// some-- it could have been script over the network over the internet somewhere, but in this case, for simplicity sake, I have use-- it's in the same folder structure and it's called ajax.php. The next remaining few lines pertained to the actual xhr object which in this case is the XMLHttpRequest object. So the remainder of this lines and this function are all AJAX or in how AJAX works. All right. So first thing I do here is I give the value of xhr, I create an HTTP request object which returns an XMLHttpRequest object and I will give that value to the variable xhr. The next thing which is what David mentioned in class is I would create a connection to the server side script by calling the open method. So I'll do xhr.open, but in my case, I will be using POST as opposed to GET which was mentioned in class. That's-- This is the first parameter with method I could use POST, I could use GET, I can use PUT. But for this class, we'll be using mainly POST and GET. The second parameter is the url. Lastly, this third parameter that is set to true is what makes AJAX asynchronous. If I set this false, AJAX will become synchronous, and I will go over this later. The next line over specifies what kind-- what type of-- what the content type is that will be sent over. For regular text and for just plain text, you will be using most-- for the most part, application/ x-www-form-urlencoded. But for things like files, you would want to use something like multipart/form-data. Now for the POST request, you need to specify how long the length of the actual parameters to the server. And in this case, I will specify this by setting the request header with this and as a second parameter putting params.length. The next line implies that once the server, once the server has responded, close the connection with the x inside the XMLHttpRequest object. And over to the next line, onreadystatechange, I know we mentioned this in class. What happens here is that each time, each time the server does something in terms of states it goes through four different states. Each time it does something, it will-- onreadystatechange will have a-- it will, OK, it will send a notification to the XMLHttpRequest object with a value. So when it's first being initialize so forth, it will go through four different states, one, two, three and four. And what I'm specifying here is that each time the server is shifting from one state to another, go ahead and call the method, this method called getTextHandler which I define below. I could have put an anonymous function here, like as in function and so forth, but I choose to do it this way because if I wanted to enter a lot of code in this function, it would not look so visually and so aesthetically pleasing for it to do so, and hence why I put it down here. Now mind you, I cannot pass parameters here by opening parenthesis and entering something like a variable1 and so forth. This will not work. Instead if you want to-- if you choose to send something to-- send parameters to this function, what you will need to do is call an anonymous function, and then call getTextHandler inside this, and here's were you'll past the parameter like variable. This is the right way to do this. [ Pause ] Lastly, the last line here is the part which sends the parameters or sends the information over to the server and it looks like this, xhr.send and inside is where you specify your parameters to be sent to the server side. Now, I could have alternately put nothing in here or null, I could have done this or null which means that I'm not really sending anything to the server. But in this case, I am sending a variable called-- where is it, i_want_text. OK. On to the callback function which is called onreadystatechange. Does anyone have any questions so far, on this? No? OK. So this is the handler function that will be called four times by the XMLHttpRequest object. Well, four times assuming that the server made it that far. And I can-- maybe I can demonstrate this a little later but for this example, I will first just make a simple AJAX call to the server. Let me show you what ajax.php looks like. It is a very small file and I made it as such for simplicity and it either accepts-- well, I've commented code out, either accepts a POST variable called i_want_xml or i_want_text. So what I'm saying here essentially is that I'm looking for a POST variable called i_want_text. If it is available regardless of its value, I want the server to print out text received with value of whatever the value of that variable is. Back to ajax.html. OK. So how do I call this function? How do I make this happen? How can I call getText when the page loads? Can anyone tell me? OK. So when the page loads, there are two message, mainly. Well, they all point to basically the same thing. But when the actual page loads, I can use the onload event in the body. I can call it here. I can say getText. Or I can use AJAX's-- I mean, jQuery's document .ready function, and I can put getText here. And I'm able to use jQuery here because I, if you recall earlier, I specified that I called for the script to come from Google so I have the entire jQuery library at this-- I will have-- once this document loads, I will have at this point the entire jQuery library loaded in my DOM. [ Pause ] Let's go back to getText. [ Pause ] OK. So it calls for a getTextHandler. Once the page loads, the body will call getText and then it will send the information over to ajax.php. And when ajax.php starts changing states, it will call XMLHttpRequest here, it will call the getTextHandler method, which is this method. And if all is good, if readyState is equal to 4, which is the last state that the server is in, and if the status code is 200, which means everything is OK, the page was found and it's delivering the data, then I can go ahead and alert the text that was submitted back to the browser. So if I go here, if I come here to localhost/section_ajax.html and I go ahead and refresh the page, what should I see based on these codes? [ Pause ] Let me go back a little bit. So I am sending i_want_text equals to yes to the server. The server is as a POST variable. The server is taking this and is printing this value out. And then, the handler, which is down here, is going to alert in a text box the response text, so what should I see? What should happen when I load the page? I'm sorry? Alert? An alert? An alert, oh, yes, an alert, but what is inside this alert box? Yes. Text received with value of. Text received with value, yes. Let's see. OK. Nothing happened. Oh, there you go. It took a long time, considering it's on the same folder. But this is what-- this is the text that is being responded back from the server code. OK. Is everyone clear on this part of the code? This part? OK, good. Now we're going to move over to another example. I call this one getXMLObject because-- and this time around, everything is pretty much the same except that the parameters-- the parameter name is i_want_xml and I will uncomment that part in the corresponding server side. And the response type is actual response as of XML. So, the difference here is that I will be receiving an XML structure. Let me go back to ajax.php and I will comment this out and I'll tell you why I'm commenting this out shortly. And I will comment this in. So if POST-- if it says, I want-- if the variable is called i_want_xml, if that's being sent over to me as a server script, I want to send out an XML structure. OK. Where are you? OK. So here, this should alert the response that XML coming from the server, I mean the .responseXML coming from the server. What should we-- now, oh, I need to call the function first, so getXMLObject. I apologize for scrolling down so much. getXMLObject, let me remove this one. OK. What do you expect to happen now when I load the page and I'll go back up and-- we'll go back up here, right. What do you expect to see happen when I alert xhr.responseXML? And this is the actual response coming from the server. [ Pause ] [ Inaudible Remark ] Sorry, what was that? [ Inaudible Remark ] It will alert the XML in the text box. That's pretty close, but in fact, it will not. And what it will alert-- this might get, take some time. It will actually alert that it's an object-- it's an XML document object because it is enveloped in an actual object. It is not raw text. It's not plain text. So, which takes us to the next-- to our next function called-- on this client side, of course-- and that is called getXMLText. Now with this iteration, what is essentially happening is that in the response-- on the response end, I am taking the response, the XML structure, and I am getting all the tags called text because I've specified-- under the root note, I've specified a bunch of values enclosed within text and labeled tags. So here, I'm getting all the tags called text and inserting them into this variable and this variable becomes an array holding all these values. So let's suppose now I alert-- I'm going to alert the text_tag_values.length. I will first need to change this to getXMLText. OK. What do you expect to see happen now when I call this and I know you're not looking at the PHP side, but now that you look-- you can see here, I have two text elements and I am calling for GET to get all the text that are named text and inserting them into the variable, into this variable, text_tag_values. What should I see when I call for the length of this array, how many values or if any? Two? Two? Two is the correct answer. There it is. That's two elements. Now, how do I print them out? Let's go ahead and iterate through this XML and through this array. Or let's say, i=0;i and call this, thank you genie. OK. That's it. If I refresh this without closing and I click on the info-- on the marker, I should see this is in blue and in h1 font. OK. OK, any questions? This pretty much covers-- I think it touches on the most important aspects of project 2. Any question at all? Is everything really clear? Yes. Can you explain a little bit about the cache? Oh yeah, sure absolutely. What was the question? Oh, the question. Can you explain the mechanism of the cache and how will it work. So the cache. The BART, their API has a set of-- let me show you what it looks like, first of all. So if you look at BART's documentation, their API. If I click on, for example, Route Information, OK, and if I click on routes, it will respond back with an XML structure of their routes and so forth. And down here is a sample of what you will get back as a response from this-- from their servers, OK? So you will need to take this information from BART's Real API, whatever they're sending back. Now, notice that there are so many different pieces of information here. You will need to take the information, parse out the information that you need and save it locally in some logical structure, OK? So what kind of information do you think you would need from BART? There are so many things that you can get from there. You can't get through it unless you can get route info, advisories and so forth. What-- The answer to this is you will need to get just enough information to accomplish the polylines and the markers. And in fact, there is a way we can test this out here. Let's see if I can find it. There's a place on the left side that says examples or-- you can actually click on this. [ Pause ] Maybe under Overview. Route Information, Examples, here we go. So examples, let's click on-- there we go. You see this? This is what I'm getting back from their API. And you will need to do such a thing in two parts of this project. First part is when you get the information to cache. The second part is where you will be asking it to-- asking for the real time arrivals and departures information. And there are two ways to get the real time arrivals and departures. There's a simple ETA feed. It's somewhere on their site and there's also the-- you can use the Real BART API to get the real time-- those figures. I don't know if I've answered your question but did it-- did the cache make sense? Or do you want me to go to more detail? Yeah, it has. Thanks. Do you want me to go into more detail on how to store the cache or how to store the information? Yeah, yeah, get on. So you're making a call to the-- to the Real Bart API first, right? You're getting the information that you need to-- you're getting all the information about the routes, their locations, all the stations, their locations, the different routes and so forth. And this would be up to you to decide how to put everything conceptually on your end. Now, locally, what you do is you make the call. You can actually-- You can take the values and copy them and paste them, but that's a lot of work. Or you can create a script, a PHP script that will go out and pull all this information locally. You can store it in an XML file, you can store it in JSON-- CSV file, and so forth. I recommend that you use a MySQL database to store it locally. Because it's, as I mentioned earlier, it will have-- it has-- their API already has a relational structure. So it's much easier to manipulate later. So you'll start in a bunch of, let's say, tables or files and so forth. Then you will store in a way that you understand and that you were able to extract from later to accomplish the polylines and the markers. But the-- How is your time on the arrival time? They are-- They keep changing, right? So those, you should get real time. Yes, the depart-- So the question is-- the suggestion was that they keep changing, the departure and arrival times keep changing. Yes, those will be called in real time. So when you click on that marker, a real time request, a request, let's say a POST or a GET will go to the server, to their server and it will receive the XML response. And then you will parse the XML response and we'll throw it into that info window before it is being displayed. Does that make sense? Yeah, thank you. But in doing so and this-- for this part there are two ways to do it. There are two-- BART has two ways to feed you this information. One is to throw their Real BART API and the other one is called a simple ETA feeds. You could look for that online. I personally use the Real BART API. Any other questions? Is everything pretty much clear? Yes? Has it been over an hour? Yes. Sorry. OK. Then, I guess, we can conclude this session. I hope I touched on the important parts of AJAX and the walkthrough for project 2. If you have any questions, feel free to use the discussion board. And after class, Chris Gerber will be handling on campus office hours and at 9 p.m., I will be holding a session for online office hours on TeamViewer and the information is provided to you on the course's website under the Google Calendar. There's the TeamViewer ID and so forth, everything that you need to connect to that. So thank you everyone and good luck on project-- both projects 1 and 2. Let us know if you have any questions. [ Applause ] The clap always makes me laugh, you know? [ Silence ]