Data Persistence This week use what you have learned about form processing, cook
ID: 3714678 • Letter: D
Question
Data Persistence
This week use what you have learned about form processing, cookies, and sessions to build a simple data persistence application. Your task is to read and write data coming from and HTML form (provided) into either a cookie or into a session. A radio button in the HTML form is used to indicate which data store is to be used. The point the of labs is to practice with reading and writing out of these different data stores, and learn how they can be used to share data between page loads. On each page load, do the following: Does a form need to be processed (did someone post information to me)? If so, process it into either the cookie or a session. Is there data already in the session? If so you need to print it out. Is there data in a cookie? Is so you need to print it out. In the zip file attached is one file: persistence.php - This file in the template application file. Much of the code you will need in already in here (HTML, CSS, and some essential PHP). The expectations and grade rubric For a perfect grade: All data from the form must be saved into the selected data store (session or cookie) All data is printed to the screen in the HTML format provided. The application throws no errors (warnings are okay) No entries are hard coded in HTML (that's cheating)
Attached Files:
<?php
# I'm going to silence warnings for you... you will gerate a ton of them.
# If you are having problem with your code, comment this line out so you can see the warnings.
error_reporting(E_ALL ^ E_WARNING);
# First thing you need to do it start the session, this should alwasy be done first.
session_start();
# Next, get whatever is currently in the session so you can spit out on the page
$session_data = $_SESSION;
# Next, get whatever is currently in the cookies so you can spit out on the page
$cookie_data = $_COOKIE;
# Next, you need to see if the form has posted data to this page
if(!empty($_POST)){
# Now you need to check to see if the check boxes are checked
if($_POST['save_in_session'] == 1){
# If you got this far, then you need to save the rest of the form data into the session
}
if($_POST['save_cookie'] == 1){
# If you got this far, then you need to save the rest of the form data into cookies
}
}
# Now that you have saved what you needed to, spit it out on the session and cookie data below.
# There are already placeholders for the data.
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Phone Book</title>
</head>
<body>
<h1>Data persistence with Sessions and Cookies</h1>
<hr>
<div>
<h2>Enter new information</h2>
<form action='#' method="post">
<table border="0" width="">
<tr>
<td>Name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Street:</td>
<td><input type="text" name="street"></td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" name="city"></td>
</tr>
<tr>
<td>State:</td>
<td><input type="text" name="state"></td>
</tr>
<tr>
<td>Zip:</td>
<td><input type="text" name="zip"></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email"></td>
</tr>
</table>
<label>Save in Session: <input name="save_in_session" type="checkbox" value="1"></label>
<br>
<label>Save Cookie: <input name="save_cookie" type="checkbox" value="1"></label>
<br>
<input type="submit" value="submit"/>
</form>
</div>
<hr>
<div id="" class="">
<h2>Information in Session</h2>
<table border="0" width="">
<tr>
<td>Name:</td>
<td> </td>
</tr>
<tr>
<td>Street:</td>
<td> </td>
</tr>
<tr>
<td>City:</td>
<td> </td>
</tr>
<tr>
<td>State:</td>
<td> </td>
</tr>
<tr>
<td>Zip:</td>
<td> </td>
</tr>
<tr>
<td>Phone:</td>
<td> </td>
</tr>
<tr>
<td>Email</td>
<td> </td>
</tr>
</table>
</div>
<div id="" class="">
<h2>Information in Cookies</h2>
<table border="0" width="">
<tr>
<td>Name:</td>
<td> </td>
</tr>
<tr>
<td>Street:</td>
<td> </td>
</tr>
<tr>
<td>City:</td>
<td> </td>
</tr>
<tr>
<td>State:</td>
<td> </td>
</tr>
<tr>
<td>Zip:</td>
<td> </td>
</tr>
<tr>
<td>Phone:</td>
<td> </td>
</tr>
<tr>
<td>Email</td>
<td> </td>
</tr>
</table>
</div>
<div></div>
</body>
</html>
Explanation / Answer
The solution is very simple. What is required is only to create a session object and a cookie object using $_SESSION[] and $_COOKIE[] respectively.Mind well,there is a separate session and cookie object for each from data. After submitting the form,you check whether if there is any data submitted;if it is then store them into the Cookies and session objects and finally display them using a php echo statement. I am putting the code below:
<?php
# I'm going to silence warnings for you... you will gerate a ton of them.
# If you are having problem with your code, comment this line out so you can see the warnings.
error_reporting(E_ALL ^ E_WARNING);
session_start();
$session_data = $_SESSION;
$cookie_data = $_COOKIE;
if(!empty($_POST)){
if($_POST['save_in_session'] == 1){
$_SESSION["name"]=$_POST["name"];
$_SESSION["street"]=$_POST["street"];
$_SESSION["city"]=$_POST["city"];
$_SESSION["state"]=$_POST["state"];
$_SESSION["zip"]=$_POST["zip"];
$_SESSION["phone"]=$_POST["phone"];
$_SESSION["email"]=$_POST["email"];
}
if($_POST['save_cookie'] == 1)
$_COOKIE["name"]=$_POST["name"];
$_COOKIE["street"]=$_POST["street"];
$_COOKIE["city"]=$_POST["city"];
$_COOKIE["state"]=$_POST["state"];
$_COOKIE["zip"]=$_POST["zip"];
$_COOKIE["phone"]=$_POST["phone"];
$_COOKIE["email"]=$_POST["email"];
}
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Phone Book</title>
</head>
<body>
<h1>Data persistence with Sessions and Cookies</h1>
<hr>
<div>
<h2>Enter new information</h2>
<form action='#' method="post">
<table border="0" width="">
<tr>
<td>Name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Street:</td>
<td><input type="text" name="street"></td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" name="city"></td>
</tr>
<tr>
<td>State:</td>
<td><input type="text" name="state"></td>
</tr>
<tr>
<td>Zip:</td>
<td><input type="text" name="zip"></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email"></td>
</tr>
</table>
<label>Save in Session: <input name="save_in_session" type="checkbox" value="1"></label>
<br>
<label>Save Cookie: <input name="save_cookie" type="checkbox" value="1"></label>
<br>
<input type="submit" value="submit"/>
</form>
</div>
<hr>
<div id="" class="">
<h2>Information in Session</h2>
<table border="0" width="">
<tr>
<td>Name:</td>
<td><?php echo $_SESSION["name"] ?></td>
</tr>
<tr>
<td>Street:</td>
<td><?php echo $_SESSION["street"] ?></td>
</tr>
<tr>
<td>City:</td>
<td><?php echo $_SESSION["city"] ?></td>
</tr>
<tr>
<td>State:</td>
<td><?php echo $_SESSION["state"] ?></td>
</tr>
<tr>
<td>Zip:</td>
<td><?php echo $_SESSION["zip"] ?></td>
</tr>
<tr>
<td>Phone:</td>
<td><?php echo $_SESSION["phone"] ?></td>
</tr>
<tr>
<td>Email</td>
<td><?php echo $_SESSION["email"] ?></td>
</tr>
</table>
</div>
<div id="" class="">
<h2>Information in Cookies</h2>
<table border="0" width="">
<tr>
<td>Name:</td>
<td><?php echo $_COOKIE["name"] ?></td>
</tr>
<tr>
<td>Street:</td>
<td><?php echo $_COOKIE["street"] ?></td>
</tr>
<tr>
<td>City:</td>
<td><?php echo $_COOKIE["city"] ?></td>
</tr>
<tr>
<td>State:</td>
<td><?php echo $_COOKIE["state"] ?></td>
</tr>
<tr>
<td>Zip:</td>
<td><?php echo $_COOKIE["zip"] ?></td>
</tr>
<tr>
<td>Phone:</td>
<td><?php echo $_COOKIE["phone"] ?></td>
</tr>
<tr>
<td>Email</td>
<td><?php echo $_COOKIE["email"] ?></td>
</tr>
</table>
</div>
<div></div>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.