使用php在下拉菜单中缩写状态

时间:2015-11-20 03:10:03

标签: php database text

我又回来了,我知道你们可能已经厌倦了我大声笑但是有些事情我想出了如何添加文本框。我仍然遇到下拉菜单的问题。我应该有一个下拉菜单,其中包含状态列表,当您提交时,它将缩写,但不想为我工作。我很确定它很容易修复,我一直在寻找它。

 <!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>

2 个答案:

答案 0 :(得分:1)

试试这个:

    $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>

答案 1 :(得分:0)

这就是code indentation如此重要的原因..你错过了在两个地方关闭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>