好吧,伙计们,基本上我有一个表格。当用户单击提交时,此表单应输出用户选择或文本,还应输出用户选择的背景颜色。
目前正在运行的是文本...和复选框。我无法得到输出的下拉选择...当涉及到背景颜色......我不知道从哪里开始..有点冗长...对不起,只需要一些方向。它应该在输出上看到的一个例子是:http://omega.uta.edu/~cyjang/ctec4309/labex/php2/post_form_3a.php 一旦你填写完毕,你就会看到......
这是我的HTML:
<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="mycity">
<option value="Arlington" name"city">Arlington</option>
<option value="Dallas" name"city">Dallas</option>
<option value="FTW" name"city">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>
,这是我的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', 'city');
$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 = "<p> <table border=2 cellpadding=5>
<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>.";
}
?>
答案 0 :(得分:2)
在下拉列表中,<option>
只能有value=""
属性,而不是name=""
。该名称只能位于<select>
部分:
<select name="city">
<option value="Arlington">Arlington</option>
<option value="Dallas">Dallas</option>
<option value="FTW">Fort Worth</option>
</select>
对于颜色,只需使用简单的样式表(css
):
$output = "
<style>
th,td { background-color: $bgcolor; }
</style>";
或者,如果您创建一个名为“yellow”和“blue”的css类,则可以指定该类:
$output .= "
<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>";
CSS文件:
table.yellow td,
table.yellow th { background-color: #FFFF00; }
table.blue td,
table.blue th { background-color: #0000FF; }