>> SPEAKER 1: Let's implement a web page that allows users to
look up stock quotes.
Let's suppose first that there exists a file called quote.php, but this
could be implemented in most any language, but whose purpose in life is
to expect an HTTP get parameter called symbol, the value of which is the
symbol of the stock for which you'd like a quote.
>> For instance, if we look at this file in my browser, quote.php, notice that
I've passed in, already, a symbol of FB, for Facebook.
And notice that what has come back is a JSON object with three keys.
One's called symbol, one called name, and one called price.
The values of which are exactly that from the file in question.
>> Now let's turn our attention to the beginnings of some HTML and
JavaScript.
Here, in ajax-0.html, notice that inside of my page's body I have a form
that has an onsubmit attribute, the value of which is quote and return
false, thereby specifying that I'd like to call a JavaScript function
called quote and then return false.
Meanwhile, inside of that form is a symbol whose ID is, quote unquote,
"symbol," and then another input whose type is submit that gives me a
submission button.
>> Meanwhile, inside of my page's head, there is a script tag, the source of
which is the URL to jquery, the very popular JavaScript library, and below
that is another script tag, inside of which is the beginnings of some
JavaScript that I myself will write.
This is the quote function that will ultimately be called, and inside of
here let's declare a variable called URL, specified that the value of this
variable shall be quote.php?symbol=".
>> And now let's concatenate onto the end of that initial string whatever symbol
the user has presumably typed.
Specifically, let's do +$""#symbol thereby specifying that I'd like to
get, with jquery from my DOM, the element whose unique
identifier, a symbol.
Recall that that is simply the text field into which the user is going to
type that stock symbol.
>> Then let's call val, which is a function or method that will get the
value that the user's typed in.
And then let's call a jquery function called getJSON, passing in as the
first argument that URL, passing in, as the second argument, an anonymous
function that expects a single argument.
Data, we'll call it.
And inside of this anonymous function, we'll simply do alert, and then in
parentheses, data.price.
If data, after all, is that JSON object with three fields--
symbol, name, and price, by doing data.price I'll get
exactly that value.
>> Let's now save the file and open it up in a browser.
http://localhost/ajax-0.html.
Here we have that form.
Let's type in FB for Facebook and get quote.
And there, then, is Facebook's latest stock price.