The purpose of this lab is to work with forms and arrays 1 R

The purpose of this lab is to work with forms and arrays.

1. Refactor the code so that the moments() function is in a separate file named functions.php
require() this file in lab3.php
2. Modfiy the form so that it POSTs to itself – lab3.php
   be sure to also add in the name attributes for the input elements
3. All input fields are required. If an input field is missing, display an error message
4. If all input fields are provided, display a thank you message with the authors full name and current time.
ie. this will do fine Thank you Gary Tong for posting! September 16th, 2016.

-----------------------------------------------------------------

// below is the starter kit file lab3.php

<?php

function moments($seconds)

{

if($seconds < 60 * 60 * 24 * 30)

{

return \"within the month\";

}

return \"a while ago\";

}

// do not modify these variables

$postedTime = 1481808630;

$author = \" gary TonG \";

$content = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum tincidunt est vitae ultrices accumsan. Aliquam ornare lacus adipiscing, posuere lectus et, fringilla augue.\";

// modify these variables

$formattedAuthor = trim(ucwords(strtolower($author)));

$formattedCurrentTime = date(\'l F \\t\\h\\e dS, Y\', time());

$formattedPostedTime = date(\'l F \\t\\h\\e dS, Y\', $postedTime);

$moment = moments(time() - $postedTime);

?>

<!DOCTYPE html>

<html>

<head>

<title>COMP 3015</title>

<link href=\"css/bootstrap.min.css\" rel=\"stylesheet\">

<link href=\"css/font-awesome.min.css\" rel=\"stylesheet\" type=\"text/css\">

<link href=\"css/style.css\" rel=\"stylesheet\">

</head>

<body>

<div id=\"wrapper\">

<div class=\"container\">

<div class=\"row\">

<div class=\"col-md-6 col-md-offset-3\">

<h3 class=\"login-panel text-center text-muted\">It is now <?php echo $formattedCurrentTime; ?></h3>

</div>

</div>

<div class=\"row\">

<div class=\"col-md-6 col-md-offset-3\">

<button class=\"btn btn-default\" data-toggle=\"modal\" data-target=\"#newPost\">New Post</button>

<hr/>

</div>

</div>

<div class=\"row\">

<div class=\"col-md-6 col-md-offset-3\">

<div class=\"panel panel-info\">

<div class=\"panel-heading\">

<span>

First Post!

</span>

<span class=\"pull-right text-muted\">

<?php echo $moment; ?>

</span>

</div>

<div class=\"panel-body\">

<p class=\"text-muted\">Posted on

<?php echo $formattedPostedTime; ?>

</p>

<p>

<?php echo $content; ?>

</p>

</div>

<div class=\"panel-footer\">

<p> By

<?php echo $formattedAuthor; ?>

</p>

</div>

</div>

</div>

</div>

</div>

</div>

<script src=\"js/jquery.min.js\"></script>

<script src=\"js/bootstrap.min.js\"></script>

<div id=\"newPost\" class=\"modal fade\" tabindex=\"-1\" role=\"dialog\">

<div class=\"modal-dialog\" role=\"document\">

<form role=\"form\">

<div class=\"modal-content\">

<div class=\"modal-header\">

<button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>

<h4 class=\"modal-title\">New Post</h4>

</div>

<div class=\"modal-body\">

<div class=\"form-group\">

<input class=\"form-control\" placeholder=\"First Name\">

</div>

<div class=\"form-group\">

<input class=\"form-control\" placeholder=\"Last Name\">

</div>

<div class=\"form-group\">

<label>Comment</label>

<textarea class=\"form-control\" rows=\"3\"></textarea>

</div>

<div class=\"form-group\">

<label>Priority</label>

<select class=\"form-control\">

<option value=\"1\">Important</option>

<option value=\"2\">High</option>

<option value=\"3\">Normal</option>

</select>

</div>

</div>

<div class=\"modal-footer\">

<button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Close</button>

<input type=\"submit\" class=\"btn btn-primary\"/>

</div>

</div><!-- /.modal-content -->

</form>

</div><!-- /.modal-dialog -->

</div><!-- /.modal -->

</body>

</html>

Solution

Changes made are highlighted in bold.

----------------------------------------------------------------------------------------------------------------------------------

FILENAME: moments.php

function moments($seconds)
{
if($seconds < 60 * 60 * 24 * 30)
{
return \"within the month\";
}
return \"a while ago\";
}

------------------------------------------------------------------------------------------------------------------------------------------------

FILENAME: lab3.php

<?php
require(\'moments.php\'); //include moments.php
if(isset($_POST[\"submit\"]))   //Form is submitting to itself so check if submit is true
{
if(!(isset($_POST[\"fname\"]) && isset($_POST[\"lname\"]) && isset($_POST[\"pri\"]) && isset($_POST[\"comments\"])))
{
echo \"Error!!. Please submit all the fields.\"; //If all fields are not submitted return error.
}
else
{
$postedTime = 1481808630;
$author = \" gary TonG \";
$content = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum tincidunt est vitae ultrices accumsan. Aliquam ornare lacus adipiscing, posuere lectus et, fringilla augue.\";
$postedTime = time();
$author = $_POST[\"fname\"].\" \".$_POST[\"lname\"];
$content = $_POST[\"comments\"];
// modify these variables
$formattedAuthor = trim(ucwords(strtolower($author)));
$formattedCurrentTime = date(\'l F \\t\\h\\e dS, Y\', time());
$formattedPostedTime = date(\'l F \\t\\h\\e dS, Y\', $postedTime);
$moment = moments(time() - $postedTime);
echo \"Thank You $author for posting! $formattedPostedTime\";
}
}
?>

<!DOCTYPE html>
<html>

.

. <!-- Unchanged part as in question trimmed -->

.

<form action = \"$_SERVER[PHP_SELF]\" method = \"post\" role=\"form\">

<div class=\"modal-content\">
<div class=\"modal-header\">
<button type=\"button\" name=\"close\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>
<h4 class=\"modal-title\">New Post</h4>
</div>
<div class=\"modal-body\">
<div class=\"form-group\">
<input name=\"fname\" class=\"form-control\" placeholder=\"First Name\">
</div>
<div class=\"form-group\">
<input name=\"lname\" class=\"form-control\" placeholder=\"Last Name\">
</div>
<div class=\"form-group\">
<label>Comment</label>
<textarea name=\"comment\" class=\"form-control\" rows=\"3\"></textarea>
</div>
<div class=\"form-group\">
<label>Priority</label>
<select name=\"pri\" class=\"form-control\">
<option value=\"1\">Important</option>
<option value=\"2\">High</option>
<option value=\"3\">Normal</option>
</select>
</div>
</div>
<div class=\"modal-footer\">
<button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Close</button>
<input type=\"submit\" name=\"submit\" class=\"btn btn-primary\"/>
</div>
</div><!-- /.modal-content -->
</form>
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</body>
</html>

The purpose of this lab is to work with forms and arrays. 1. Refactor the code so that the moments() function is in a separate file named functions.php require(
The purpose of this lab is to work with forms and arrays. 1. Refactor the code so that the moments() function is in a separate file named functions.php require(
The purpose of this lab is to work with forms and arrays. 1. Refactor the code so that the moments() function is in a separate file named functions.php require(
The purpose of this lab is to work with forms and arrays. 1. Refactor the code so that the moments() function is in a separate file named functions.php require(
The purpose of this lab is to work with forms and arrays. 1. Refactor the code so that the moments() function is in a separate file named functions.php require(
The purpose of this lab is to work with forms and arrays. 1. Refactor the code so that the moments() function is in a separate file named functions.php require(

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site