Code: Select all
<style>
table.yellow td,
table.yellow th {background-color: #ffff00;}
table.blue td,
table.blue th {background-color: #0000ff;}
</style>
<?php
//--------------------------
// Form Processing Script
// This script is written to work with the form in post_form_3.php.
//--------------------------
//==========================
// Data validation
//==========================
// check to see if there is a form submission or not
if (array_key_exists("SubmitThis", $_POST)) {
// data validation
// - check required fields
//== Modify the required and expected arrays below to fit your form ========
$required = array('title', 'author','comment','bgcolor');
$expected = array('title', 'author','comment','tag', 'email', 'city');
$missing = array();
// use foreach loop to run through each item in the expected array
foreach($expected as $thisField) {
// setup a variable to store user input for this field name
$thisUserInput = $_POST[$thisField];
// check if this field is a required field
if (in_array($thisField, $required)) {
// check if user input of this field is empty, if yes, add this field to the missing array
if (empty($thisUserInput)) {
array_push($missing, $thisField);
} else {
${$thisField} = $thisUserInput;
}
} else {
${$thisField} = $thisUserInput;
}
}
// after running through all expected fields, check the $missing array. if there is no required field missing, the $missing array will be empty.
if (empty($missing)){
// empty($missing) is true --> no missing field, proceed with business processes (in this example, display all user input.)
// deal with array input, ex. $tag
$tagStr = implode(", ", $tag);
// print_r ($tag); // enable this line will print the $tag array, so you can see what's been stored in the $tag array. It may help you to debug.
// process author name and email
if (!empty($email)) {
$author = "<a href='mailto:$email'>$author</a>";
}
$output = "
<style>
th,td { background-color: $bgcolor;}
</style>";
$output .= "<p> <table border=2 cellpadding=5 class=\"$bgcolor\">
<tr><th> Author:</th><td> $author </td></tr>
<tr><th> Title:</th><td> $title </td></tr>
<tr><th> Tag:</th><td> $tagStr </td></tr>
<tr><th> City:</th><td> $city </td></tr>
<tr><th> Comment:</th><td> <br>$comment </td></tr>
</table></p>";
} else {
// empty($missing) is false --> $missing array is not empty -- prepare a message for the user
$missingFieldList = implode(", ",$missing);
$output = "The following fields are missing from your post, please go back and fill them in. Thank you. <br>
<b>Missing fields: $missingFieldList </b>
";
}
} else {
$output = "Please post your message use <a href='post_form_3.php'>this form</a>.";
}
?>
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE> CTEC 4309 Class Working File: Message Form Processing </TITLE>
</HEAD>
<BODY>
CTEC 4309 Class Working File
<hr>
<h2>Preview Your Message</h2>
<hr size="1">
<p>
<?php echo $output ?>
</p>
</BODY>
</HTML>
Code: Select all
<!DOCTYPE HTML>
<HTML>
<HEAD>
<style>
table.yellow td,
table.yellow th {background-color: #ffff00;}
table.blue td,
table.blue th {background-color: #0000ff;}
</style>
<TITLE> CTEC 4309 Class Working File: Posting a Message </TITLE>
</HEAD>
<BODY>
CTEC 4309 Class Working File
<hr>
<h2>Post a Message</h2>
<hr size="1">
<h3>Form</h3>
* required fields
<form action= "post3.php" method="post">
Author * : <input type="text" name="author"><br/>
Email : <input type="text" name="email"><br/>
Title * : <input type="text" name="title"><br/>
Tag:
<input type="checkbox" name="tag[]" value="General Interests"> General Interests
<input type="checkbox" name="tag[]" value="Local Schools"> Local Schools
<input type="checkbox" name="tag[]" value="Safety"> Safety
<br/>
City *:
<select name= "city">
<option value="Arlington" >Arlington</option>
<option value="Dallas" >Dallas</option>
<option value="FTW" >Fort Worth</option>
</select>
<br/>
Background color *:
<input type="radio" name="bgcolor" value"yellow"> Yellow
<input type="radio" name="bgcolor" value"blue"> Blue
<br/>
Comment * : <br/><textarea name="comment" rows="5" cols="40"></textarea><br>
<input type="Submit" name="SubmitThis" value="Preview">
</form>
</BODY>
</HTML>