welcome.php
and save it in your
lab6
folder.
welcome.php
to your lab6
folder.
lab6
folder
to the remote server and then disconnect from the server.
http://yourid.sienasellbacks.com/lab6/welcome.php
where yourid
is the username you were emailed.
session_start()
function.
session_start()
is typically the first
line of code inside the first set of PHP tags.
$_SESSION['username'] = $_GET['username'];
$_GET['username']
is null, you do not want to set the session variable.
Welcome '.$_SESSION['username'].'
welcome.php
lab6
folder to the server. Disconnect from the server.
http://yourid.sienasellbacks.com/lab6/welcome.php
in Chrome
welcome.php
in Notepad++
else if
clause to handle when the user clicks "Add Size"echo '<h2 style="font-size: '.$_SESSION['size'].'px;
$datastring = $_SESSION['username'].",".$_SESSION['color'].",".$_SESSION['size']."\n";
$filename = "welcomedata.txt"; $myfile = fopen($filename, "a"); fwrite($myfile, $datastring); fclose($myfile);
welcome.php
lab6
folder to the server. Disconnect from the server.
http://yourid.sienasellbacks.com/lab6/welcome.php
in Chrome
The welcome.php
script shows a typical PHP application that generates or modifies
a series of forms. It also illustrates how PHP can dynamically change a web page's content and
style based on submitted user information.
PHP can receive form data easily using the $_GET variable and your application code remains hidden making it difficult for others to "steal" your core algorithms. PHP can store information temporarily via SESSIONS and permanently via files and databases.
Updating a page requires a full HTTP request/response cycle where an
entire page has to be transmitted, loaded and rendered. In fact,
the extended welcome.php
application requires
4 HTTP request/response cycles (one to load the initial form
and one for each of the three entered values).
Generating web pages with PHP can lead to an unstructured mix of hard-coded HTML/CSS and dynamically-generated HTML/CSS.
Rather than generate and control the form with PHP, we can implement the form and all the behavior
using the following JavaScript application:
welcome.html
This JavaScript mini-application will send the submitted data using AJAX to the following server-side script:
add_welcome_data.php
This script writes to the same file as the PHP script.
madlibs.php
and save it in your
lab6
folder.
madlibs.php
to your lab6
folder.
lab6
folder
to the remote server and then disconnect from the server.
http://yourid.sienasellbacks.com/lab6/madlibs.php
where yourid
is the username you were emailed.
$_POST
variable instead of the $_GET
variable.
$_SESSION
variables:
<?php session_start(); $_SESSION['name'] = $_POST['name']; $_SESSION['verb'] = $_POST['verb']; ?>
<input type="text" name="name" value="<? echo $_SESSION['name'] ?>">
$name = $_SESSION['name'];
madlibs.php
lab6
folder to the server.
http://yourid.sienasellbacks.com/lab6/madlibs.php
in Chrome
madlibs.php
so that you can enter six word and can select three different sentences:else if
to generate a third sentence from the six words.
$sentence
and append it to a file as follows:
$filename = "sentencedata.txt"; $myfile = fopen($filename, "a"); fwrite($myfile, $sentence); fclose($myfile);
madlibs.php
lab6
folder to the server.
http://yourid.sienasellbacks.com/lab6/madlibs.php
in Chrome
Rather than generate and control the form with PHP, we can implement the form and all the behavior using JavaScript.
Using the JavaScript Welcome application as a model (see below), implement the Mad Libs application using
JavaScript and AJAX:
welcome.html
Your file should be called madlibs.html
. Use the welcome.html
application as a model.
This JavaScript mini-application will send the submitted sentences using AJAX to a server-side script:
add_sentence_data.php
add_sentence_data.php
and save it in your
lab6
folder.
This script writes to the same file as the extended PHP application.
Unlike the Welcome application which sent an object with three fields, this application sends a single string. Thus, you do not have to use JSON to stringify the data object.
Create a zip file of your lab6
folder called lab6.zip
and submit the file in Blackboard.
In the comment area of Blackboard put your partner's name.
The deliverable must be submitted in Blackboard by midnight on the day before your next scheduled lab meeting. If you wish to work alone, you can submit the deliverable yourself. However, you are encouraged to work with your partner and you can submit together. When submitting as a lab pair, be sure to include your partner's name in the comment area when you submit via Blackboard.
Outside of lab, you are only permitted to work with your lab partner. Do not share your work with any other student.