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

In this PHP exercise, you will request input from the user, then move the user\'

ID: 3765678 • Letter: I

Question

In this PHP exercise, you will request input from the user, then move the user's response from one file to another and do something with it. Create two separate files. The first will contain a form with three input fields asking for the user's favorite city and the users name and email. Use the post method for the form. Although this file contains no PHP code, on my localhost, it needs the .php extension to successfully call the second file. The second file will contain PHP code to process the user's response. After the user clicks the submit button, echo back Your favorite city is $city and your name is $name., where $city and $name is the input from the form. For the email address, check that it is valid and echo the response with the email entered and the text color to be green if valid and red if not valid. ( See 19.8, page 707 of your text book for reference for this feature of checking the string ) Finally, use a php function to print the date and time. Hint: the variable that contains the user's input is an array. The array variable is $_POST['name'], where 'name' is the name of your input field.

Explanation / Answer

Index.php:

<!DOCTYPE html>

<html>
<head>
<style type="text/css">

.mail {
margin: auto;
padding-top: 10px;
padding-bottom: 10px;
width: 400px;
background : #D8F1F8;
border: 1px soild silver;
}
.mail h2 {
margin-left: 38px;
}
input {
font-size: 20pt;
}
input:focus, textarea:focus{
background-color: lightyellow;
}
input submit {
font-size: 12pt;
}
.rq {
color: #FF0000;
font-size: 10pt;
}
</style>
<script language="javascript">
<script language="javascript">
function checkEmail() {
var email = document.getElementById('email');
var filter = /^(([^<>()[].,;:s@"]+(.[^<>()[].,;:s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/;
if (!filter.test(email.value)) {
alert('Please provide a valid email address');
email.focus;
return false;
}
}
</script>
</script>
<title>Simple Form</title>
</head>

<body>
<h2>Favorite City</h2>

<form method="post" name="form1" action="response.php">
Please enter your Name: <br />
<input type="text" name="name" /><br />
Please enter your favorite city: <br />
<input type="text" name="city" /><br />
Please enter your favorite Email : <br />
<input type="text" name="email" />
<p />
<input type="submit" name="submit" value="Go"/>
</form>

</body>
</html>

response.php:

<!DOCTYPE html>

<html>
<head>

<title>Simple Response </title>
</head>

<body>
<h2>Favorite City</h2>

<?php

//Retrieve string from post submission
$name = $_POST['name'];
echo "Your Name is ".$name."<br/>";
$city = $_POST['city'];
echo "Your favorite city is ".$city."<br/>";
$email = $_POST['email'];
echo "Your Email Address is ".$email."<br/>";

?>

</body>
</html>