Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your task is to read and write data coming from and HTML form (provided) into ei

ID: 3717328 • Letter: Y

Question

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.

<?php

# 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>&nbsp;</td>

</tr>

<tr>

<td>Street:</td>

<td>&nbsp;</td>

</tr>

<tr>

<td>City:</td>

<td>&nbsp;</td>

</tr>

<tr>

<td>State:</td>

<td>&nbsp;</td>

</tr>

<tr>

<td>Zip:</td>

<td>&nbsp;</td>

</tr>

<tr>

  

<td>Phone:</td>

<td>&nbsp;</td>

</tr>

<tr>

<td>Email</td>

<td>&nbsp;</td>

</tr>

</table>

</div>

<div id="" class="">

<h2>Information in Cookies</h2>

<table border="0" width="">

<tr>

<td>Name:</td>

<td>&nbsp;</td>

</tr>

<tr>

<td>Street:</td>

<td>&nbsp;</td>

</tr>

<tr>

<td>City:</td>

<td>&nbsp;</td>

</tr>

<tr>

<td>State:</td>

<td>&nbsp;</td>

</tr>

<tr>

<td>Zip:</td>

<td>&nbsp;</td>

</tr>

<tr>

<td>Phone:</td>

<td>&nbsp;</td>

</tr>

<tr>

<td>Email</td>

<td>&nbsp;</td>

</tr>

</table>

</div>

<div></div>

</body>

</html>

Explanation / Answer

#below approach will be better choice

<?php

# 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

        $_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){

       # If you got this far, then you need to save the rest of the form data into cookies

       # best way is to use - setCookieData($_POST);

       $_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'];

    }

}

# Now that you have saved what you needed to, spit it out on the session and cookie data below.

# show all cookies using for each look

if(count($_SESSION) > 0) {

   echo "<div>";

   echo "<h2>Information in Session</h2>";

   echo "<table border='0' width=''>";

   foreach ($_SESSION as $key=>$val)

   {

       echo "<tr><td>".$key."</td><td>".$val."</td></tr> "

   }

   echo "</table>";

   echo "</div>";

}

# show all cookies using for each look

if(count($_COOKIE) > 0) {

   echo "<div>";

   echo "<h2>Information in Cookies</h2>";

   echo "<table border='0' width=''>";

   foreach ($_COOKIE as $key=>$val)

   {

       echo "<tr><td>".$key."</td><td>".$val."</td></tr> ";

   }

   echo "</table>";

   echo "</div>";

}

?>

<!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 in Cookie: <input name="save_cookie" type="checkbox" value="1"></label> <br>

           <input type="submit" value="submit" />

       </form>

   </div>

</body>

</html>