[英]Using php for abbreviating the states in a drop down menu
我又回來了,我知道你們可能厭倦了我,哈哈,但我想出了一些如何添加文本框的方法。 我仍然無法使用下拉菜單。 我應該有一個下拉菜單,其中包含州列表,當您提交時,它會被縮寫,但這也不適合我。 我很確定它很容易修復,我一直在尋找它。
<!DOCTYPE html>
<html>
<head>
<title>Lab 7, Part 1</title>
<meta charset="UTF-8"/>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
</head>
<body>
<form name="myform" action="http://weblab.kennesaw.edu/formtest.php"
onsubmit="return validateForm()"
method = "post">
<h1 style="text-align:center">Lab 7, Part 1</h1>
<h2 style="text-align:center">IT 3203</h2>
<a href="index.html"><p style="text-align:center">Main Page!</p></a>
<table>
<th>Fruits For Sale!</th>
<tr><th>Fruits</th><th>Weight</th><th>Price</th></tr>
<?php
$db=mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select fruit_item_no, fruit_name, fruit_weight, fruit_price";
$q .= " from fruit_t";
$q .= " order by fruit_name;";
$dbResult = mysqli_query($db,$q);
$num = mysqli_num_rows($dbResult);
if ($num == 0) {
echo '<tr><td colspan="2">';
echo 'Database query retrieved zero rows.</td></tr>';
}
while ($row = mysqli_fetch_assoc($dbResult)) {
$name = $row['fruit_name'];
$weight = $row['fruit_weight'];
$price = $row['fruit_price'];
echo "<tr><td><b>$name</b></td>";
echo "<td>$weight</td>";
echo "<td>$price</td>";
echo "<td><input type='text' name='name'></td></tr>\n";
}
?>
</table>
<br>
<label>First Name
<input type="text"
name="firstname" id="firstname"
size="25" />
</label>
<br>
<br>
<label>Last Name
<input type="text"
name="lastname" id="lastname"
size="25" />
</label>
<br>
<br>
<label>Street Address
<input type="text"
name="streetaddress" id="streetaddress"
size="35" />
</label>
<br>
<br>
<label>City
<input type="text"
name="city" id="city"
size="25" />
</label>
<label>State:
<select name="state" id="state">
<?php
$db=mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select state_abbr, state_name";
$q .= " from state_t";
$q .= " order by state_name;";
while ($x = mysqli_fetch_assoc($dbResult)) {
$state_abbr = $x["state_abbr"];
$state_name = $x["state_name"];
?>
<option value="<?php echo $state_abbr; ?>">
<?php echo $state_name; ?></option>
<?php
}
?>
</select>
<?php
}
?>
</label>
<br>
<br>
<label>Zip code:
<input type="text"
name="zipcode" id="zipcode"
size="20" />
</label>
<br>
<br>
<label>Visa
<input type="radio" name="pref_payment"
id="pref_payment_visa" value="visa" checked />
</label><br>
<label>MasterCard
<input type="radio" name="pref_payment"
id="pref_payment_master" value="master" checked />
</label><br>
<label>American Express
<input type="radio" name="pref_payment"
id="pref_payment_american" value="american" checked />
</label><br>
<input type="submit" value="Submit!">
</form>
</body>
</html>
嘗試這個:
$db=mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select fruit_item_no, fruit_name, fruit_weight, fruit_price";
$q .= " from fruit_t";
$q .= " order by fruit_name;";
$dbResult = mysqli_query($db,$q);
$num = mysqli_num_rows($dbResult);
if ($num == 0) {
echo '<tr><td colspan="2">';
echo 'Database query retrieved zero rows.</td></tr>';
}
while ($row = mysqli_fetch_assoc($dbResult)) {
$name = $row['fruit_name'];
$weight = $row['fruit_weight'];
$price = $row['fruit_price'];
echo "<tr><td><b>$name</b></td>";
echo "<td>$weight</td>";
echo "<td>$price</td>";
echo "<td><input type='text' name='name'></td></tr>\n";
?>
</table>
<label>State:
<select name="state" id="state">
<?php
$db=mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select state_abbr, state_name";
$q .= " from state_t";
$q .= " order by state_name;";
$dbResult = mysqli_query($db,$q);
while ($x = mysqli_fetch_assoc($dbResult)) {
$state_abbr = $x["state_abbr"];
$state_name = $x["state_name"];
?>
<option value="<?php echo $state_id; ?>">
<?php echo $state_name; ?></option>
<?php
}
?>
</select>
</label>
這就是代碼縮進如此重要的原因。您在兩個地方遺漏了關閉 PHP 標記。 參考下面的代碼。
<!DOCTYPE html>
<html>
<head>
<title>Lab 7, Part 1</title>
<meta charset="UTF-8"/>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
</head>
<body>
<form name="myform" action="http://weblab.kennesaw.edu/formtest.php" onsubmit="return validateForm()" method = "post">
<h1 style="text-align:center">Lab 7, Part 1</h1>
<h2 style="text-align:center">IT 3203</h2>
<a href="index.html"><p style="text-align:center">Main Page!</p></a>
<table>
<th>Fruits For Sale!</th>
<tr><th>Fruits</th><th>Weight</th><th>Price</th></tr>
<?php
$db = mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select fruit_item_no, fruit_name, fruit_weight, fruit_price";
$q .= " from fruit_t";
$q .= " order by fruit_name;";
$dbResult = mysqli_query($db,$q);
$num = mysqli_num_rows($dbResult);
if ($num == 0)
{
echo '<tr><td colspan="2">';
echo 'Database query retrieved zero rows.</td></tr>';
}
while ($row = mysqli_fetch_assoc($dbResult))
{
$name = $row['fruit_name'];
$weight = $row['fruit_weight'];
$price = $row['fruit_price'];
echo "<tr><td><b>$name</b></td>";
echo "<td>$weight</td>";
echo "<td>$price</td>";
echo "<td><input type='text' name='name'></td></tr>\n";
}
?>
</table>
<br>
<label>First Name
<input type="text" name="firstname" id="firstname" size="25" />
</label>
<br>
<br>
<label>Last Name
<input type="text" name="lastname" id="lastname" size="25" />
</label>
<br>
<br>
<label>Street Address
<input type="text" name="streetaddress" id="streetaddress" size="35" />
</label>
<br>
<br>
<label>City
<input type="text" name="city" id="city" size="25" />
</label>
<label>State:
<select name="state" id="state">
<?php
$db = mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select state_abbr, state_name";
$q .= " from state_t";
$q .= " order by state_name;";
$dbResult_state = mysqli_query($db,$q);
while ($x = mysqli_fetch_assoc($dbResult_state))
{
$state_abbr = $x["state_abbr"];
$state_name = $x["state_name"];
?>
<option value="<?php echo $state_abbr; ?>">
<?php echo $state_name; ?></option>
<?php
} ?>
</select>
<?php } ?> <!-- remove this -->
</label>
<br>
<br>
<label>Zip code:
<input type="text" name="zipcode" id="zipcode" size="20" />
</label>
<br>
<br>
<label>Visa
<input type="radio" name="pref_payment" id="pref_payment_visa" value="visa" checked />
</label>
<br>
<label>MasterCard
<input type="radio" name="pref_payment" id="pref_payment_master" value="master" checked />
</label>
<br>
<label>American Express
<input type="radio" name="pref_payment" id="pref_payment_american" value="american" checked />
</label>
<br>
<input type="submit" value="Submit!">
</form>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.