By the end of today, you will be able to:
Have a working PHP script that sends email to you
Use jQuery to send form data to you using that script and Ajax
We learned about two major topics: Form Submission, and Form submission via Ajax.
form
tag has an action
attribute
that specifies the script (on the server) that will get and process
the data submission
form
tag also has a method
attribute that chooses either GET
or POST
. (The default is GET
.)
This capability is called Ajax. Ajax allows a web browser to act like a desktop application. Think Gmail and Google apps.
Ajax can either be used to receive info from the server (e.g. Facebook updates, new mail from Gmail) or to send info to the server (e.g. Facebook posts, sending mail).
In this course, we'll only use it to send mail, but you should know its wider applicability.
The action
attribute specifies
what data to send
whether to send the data
where to send the data
how to send the data
To put the form data in the URL when submitting the form, the page author should use
the GET method
the POST method
either A or B
none of the above
Ajax ...
is something that happens in the browser
is something that happens on the server
is a way to pass data from the browser to the server
is a way to pass data from the server to the brower
Ajax is tricky because we can't use the following reasonable idea:
// the following does *not* work: var data = $.get("url"); processResult(data);
The reason is because of the fact that, from a computer's point of
view, it takes forever (millions of cycles) for the data to
arrive from the server. So, it doesn't want to wait that long
for .get
to return and assign a value to data
.
The browser doesn't want to freeze for all that time, waiting for the
data to arrive. It wants to pay attention to the user.
Instead, Ajax requires you to supply a function, an event
handler, which will then get invoked when the data does
arrive. This event handler is also called a callback. The
callback is invoked with the data as its argument, since that's
presumably why you requested it. In our example
above, processResult
is perfect as a callback
function. Therefore, we do:
$.get("url", processResult);
With a partner, using the following Ajax example and your JavaScript console, determine the following:
serverData
global variable.
We can also use Ajax to send data to the server. We will send data from a form, and we will use the POST method. Let's first figure out, though, the data we're going to send and how we will send it.
Let's start with specific examples from the reading.
Let's make sure the JavaScript code is clear:
function handleResponse(responseObj) { console.log("response is "+responseObj.status+" and "+responseObj.text); if( responseObj.status == "ok" ) { $("#response_element").html(responseObj.text).css("color","green"); } else { $("#response_element").html(responseObj.text).css("color","red"); } } function sendmail () { var where = $("#rsvp-form").attr("action"); var fn = $("#rsvp-form [name=from_name]").val(); var fe = $("#rsvp-form [name=from_email]").val(); var what = {from_name: fn, from_email: fe }; $.post( where, what, handleResponse, "json"); } $("#ajax_send_button").click(sendmail);
These forms claim to be RSVP, but they always say "yes I'm coming." Can we modify these forms to send a Yes/No, rather than always saying "yes"?
Yes, just add a SELECT menu to the form.
Yes, just add a BODY property to the JS object literal in the Ajax code
Yes, but only in the Ajax version; the normal version can't do that.
No, because the back-end script doesn't allow it
Both of the above forms allowed you to specify the recipient (so you could send it to yourself) but not the content. Couldn't we provide a more flexible form that would allow you to specify all the email essentials:
Yes, we could, but it would be a very bad idea. Spammers would find that form in about ten minutes and start using it to send spam. Even if you look at our PHP scripts and figure out how to do this, don't. You'll get your server blacklisted and then you won't be able to send legitimate email.
In our examples, we will restrict the email in various ways to make it unappealing to spammers. The primary way is to restrict the recipient. The bad guys could fill up the recipient's email box, but they can already do that.
The back-end script determines what form inputs are meaningful. Our email scripts look for the following:
from_name
from_email
subject
body
If additional inputs are sent, they are silently ignored.
In the two examples above, we used a different back-end script that only let you specify the first two inputs: the subject and body of the email were pre-specified.
For the next task, we'll use a script that just echoes the data back to
the browser. It's not useful for any real tasks, but scripts like this
are invaluable for debugging. Think of it as like
a console.log
for Ajax requests. Click on it!
http://cs.wellesley.edu/~cs110/php/echo.php
You can see that the script sends back a little bit of HTML.
Try the following (we are using the GET method):
echo.php
script.
Hint: use a JS object literal.
response1
. Hint: you might use
the .html()
method.
Your solution might look like this:
function responseHandler (chunk) { $("#response1").html(chunk); } $.post("http://cs.wellesley.edu/~cs110/php/echo.php", {pet: "Hedwig", house: "Gryffindor"}, responseHandler);
Suppose we give the user some choices about the body of the email but not the recipient. If we further allow ourselves to format the result more nicely than those bullet lists earlier, we can get something like the example from the reading.
We'll switch to that other page and make sure everyone is comfortable with all that code.
If you want to use this facility, we need a way for you to create the
appropriate PHP script. Rather than teach you the language just for
this one purpose, we've created a meta-script
: a script that
writes a PHP script for you. It has a form for you to fill out with
your email address and your desired default values, and then gives you a
PHP script in your Downloads
folder (or wherever your
browser puts it).
This task will ensure that the basics of email form submission work for you.
public_html
.
mail-form
folder.
mail-form
folder, so
that the relative URL for the PHP script is correct. You will now
have a folder with an HTML file and a PHP file: the HTML file has a
form that uses the PHP file to send mail to you.
We hope that after these activities you have a good understanding of
.post()
method needs:
Will be posted later, visit again after .
Quiz answers:
1. C 2. A 3. C and D. In CS110, we'll use Ajax to send data to server (option C). 4. D