我正在尝试使用php创建一个表单。我不确定如何在城市场地之后放置国家下拉菜单。我不确定最好的方法,我有我的阵列。
$labels = array (
"first_name" => "First Name",
"last_name" => "Last Name",
"address" => "Address",
"city" => "City",
"email" => "E-mail",
"phone" => "Phone", );
$country = array (
"select" => "",
"us" => "United States",
"ca" => "Canada",
"mx" => "Mexico", );
$submit = "Submit";
?>
以下是显示代码:
<?php
echo "<h2>Customer Info</h2>";
echo "<form action='checkBlank.php' method='post'>";
foreach ( $labels as $field => $label)
{
echo "<div class='field'>
<label for='$field'>$label</label>
<input id='$field' name='$field' type='text'
size='42' /></div>";
if($field == "city") {
echo "<label for='country'>Country</label>
<select id='country' name='country'>";
foreach ( $country as $select => $option)
{
echo "<option value='$value'>$option</option>";
}
echo "</select>";
}
}
echo "<div id='submit'>
<input type='submit' value='$submit'></div>
</form>";
?>
答案 0 :(得分:1)
测试字段名称,如果是城市,请执行另一个循环以显示国家/地区下拉列表。
foreach ( $labels as $field => $label)
{
echo "<div class='field'>
<label for='$field'>$label</label>
<input id='$field' name='$field' type='text'
size='42' /></div>";
if ($field == 'city') {
echo '<div><select name="country">';
foreach ($country as $short => $long) {
echo "<option value='$short'>$long</option>";
}
echo '</select></div>';
}
}
顺便说一句,对Select One
选项使用空值是常规的。如果您将字段标记为必需,标准表格验证工具会将此识别为意味着未选择任何选项。
答案 1 :(得分:0)
&#39;看起来@Barmar打败了我,但我有类似的解决方案
$labels = array (
"first_name" => "First Name",
"last_name" => "Last Name",
"address" => "Address",
"city" => "City",
"email" => "E-mail",
"phone" => "Phone", );
$country = array (
"select" => "Select One",
"us" => "United States",
"ca" => "Canada",
"mx" => "Mexico", );
$submit = "Submit";
echo "<h2>Customer Info</h2>";
echo "<form action='checkBlank.php' method='post'>";
foreach ( $labels as $field => $label)
{
echo "<div class='field'>
<label for='$field'>$label</label>
<input id='$field' name='$field' type='text'
size='42' /></div>";
if($field == "city") {
echo "<label for='country'>Country</label><select id='country' name='country'>";
foreach ( $country as $value => $option)
{
echo "<option value='$value'>$option</option>";
}
echo "</select>";
}
}
echo "<div id='submit'>
<input type='submit' value='$submit'></div>
</form>";
答案 2 :(得分:0)
我个人会这样做:
$fields = array (
array(
"name" => "first_name",
"label" => "First Name",
),
array(
"name" => "last_name",
"label" => "Last Name",
),
array(
"name" => "address",
"label" => "Address",
),
array(
"name" => "city",
"label" => "City",
),
array(
"name" => "country",
"label" => "Country",
"options" => array (
"" => "Select One",
"us" => "United States",
"ca" => "Canada",
"mx" => "Mexico",
),
),
array(
"name" => "email",
"label" => "E-mail",
),
array(
"name" => "phone",
"label" => "Phone",
),
);
foreach($fields as $field) {
echo "<div class='field'><label for='{$field['name']}'>{$field['label']}</label>";
if (isset($field['options'])) {
echo "<select name='{$field['name']}'>";
foreach ($option as $value => $title) {
echo "<option value='$value'>$title</option>";
}
echo '</select>'
} else {
echo "<input id='{$field['name']}' name='{$field['name']}' type='text' size='42' />";
}
echo "</div>";
}