使用PHP </select>访问<select>元素中所选选项的值

时间:2015-03-25 13:04:18

标签: php html html5 combobox

我正在尝试创建一个简单的表单,在提交表单时将数据发送到PHP脚本。我能够访问所有文本框值,并已成功实现每个值的变量。但是,当我尝试访问表单上的组合框的值时,我在PHP中收到此错误。

Notice: Undefined index: cmbProvince in C:\inetpub\wwwroot\krAssignment4\scripts\index.php on line 15

HTML

<form id="orderForm" action="scripts/index.php" method="post" onsubmit="return validateForm()">
        <fieldset id="customerInfo">
            <legend>Customer Information</legend>

            <label for="txtFirstName">Name:</label><br>
            <input type="text" id="txtFirstName" name="txtFirstName"
                   placeholder="First Name" autofocus="true"
                   oninput="formatFirstName()">
            <input type="text" id="txtLastName" name="txtLastName"
                   placeholder="Last Name" oninput="formatLastName()"><br><br>

            <label for="txtAddress">Address:</label><br>
            <input type="text" id="txtAddress" name="txtAddress" placeholder="Address"
                   oninput="formatAddress()"><br>
            <input type="text" id="txtCity" name="txtCity" placeholder="City"
                   oninput="formatCity()">
            <select id="cmbProvince" name="cmbProvince">
                <option value="on">ON</option>
                <option value="qc">QC</option>
                <option value="mb">MB</option>
                <option value="sk">SK</option>
                <option value="ab">AB</option>
                <option value="bc">BC</option>
                <option value="nb">NB</option>
                <option value="nl">NL</option>
                <option value="ns">NS</option>
                <option value="nu">NU</option>
                <option value="pe">PE</option>
                <option value="nt">NT</option>
                <option value="yt">YT</option>
            </select><br>
            <input type="text" id="txtPostalCode" name="txtPostalCode"
                   placeholder="Postal Code" oninput="displayPostalCode()">
            <br><br>
            <!-- Button that will display the order form -->
            <input type="button" id="btnOrder" value="Order"
                   onclick="showOrderInformation()">
        </fieldset><br>

        <fieldset id="orderInfo">
            <legend>Order Information</legend>

            <label for="cmbProduct">Product:</label><br>
            <select id="cmbProduct">
                <option> -- Select -- </option>
                <option>Blackberries</option>
            </select>
            &ensp;@&ensp;
            <input type="number" id="txtQuantity" name="txtQuantity" value="1"
                   step="1" min="1" max="50" oninput="displayTotalTax()"><br><br>
        </fieldset>
        <br><br>

        <!-- Button to submit the form to PHP after validation -->
        <input type="submit" id="btnSubmit" value="Submit">
    </form>

PHP(10-19行)

/* Capture the user input from the HTML form */
$strFirstName = $_POST['txtFirstName'];
$strLastName = $_POST['txtLastName'];
$strAddress = $_POST['txtAddress'];
$strCity = $_POST['txtCity'];
$strProvince = $_POST['cmbProvince'];
$strPostalCode = $_POST['txtPostalCode'];

我输错了什么?还是错过了什么?除了组合框之外的所有正在运行。

如果我需要包含其他任何内容,请与我们联系。谢谢!


修改

当我尝试`var_dump($ _ POST);

时会发生什么
Notice: Undefined index: cmbProvince in C:\inetpub\wwwroot\krAssignment4\scripts\index.php on line 15 
Notice: Undefined index: cmbProduct in C:\inetpub\wwwroot\krAssignment4\scripts\index.php on line 18 
array(6) { ["txtFirstName"]=> string(7) "Kendall" 
["txtLastName"]=> string(4) "Roth" 
["txtAddress"]=> string(19) "22 Resolution Drive" 
["txtCity"]=> string(8) "Millbank" 
["txtPostalCode"]=> string(7) "K2K 2K2" 
["txtQuantity"]=> string(1) "2" }

2 个答案:

答案 0 :(得分:2)

您的代码对我来说有点变化,试试这个:

    <form id="orderForm" action="scripts/index.php" method="post" onsubmit="return validateForm()">
            <fieldset id="customerInfo">
                <legend>Customer Information</legend>

                <label for="txtFirstName">Name:</label><br>
                <input type="text" id="txtFirstName" name="txtFirstName"
                       placeholder="First Name" autofocus="true"
                       oninput="formatFirstName()">
                <input type="text" id="txtLastName" name="txtLastName"
                       placeholder="Last Name" oninput="formatLastName()"><br><br>

                <label for="txtAddress">Address:</label><br>
                <input type="text" id="txtAddress" name="txtAddress" placeholder="Address"
                       oninput="formatAddress()"><br>
                <input type="text" id="txtCity" name="txtCity" placeholder="City"
                       oninput="formatCity()">
                <select id="cmbProvince" name="cmbProvince">
                    <option value="on">ON</option>
                    <option value="qc">QC</option>
                    <option value="mb">MB</option>
                    <option value="sk">SK</option>
                    <option value="ab">AB</option>
                    <option value="bc">BC</option>
                    <option value="nb">NB</option>
                    <option value="nl">NL</option>
                    <option value="ns">NS</option>
                    <option value="nu">NU</option>
                    <option value="pe">PE</option>
                    <option value="nt">NT</option>
                    <option value="yt">YT</option>
                </select><br>
                <input type="text" id="txtPostalCode" name="txtPostalCode"
                       placeholder="Postal Code" oninput="displayPostalCode()">
                <br><br>
                <!-- Button that will display the order form -->
                <input type="button" id="btnOrder" value="Order"
                       onclick="showOrderInformation()">
            </fieldset><br>

            <fieldset id="orderInfo">
                <legend>Order Information</legend>

                <label for="cmbProduct">Product:</label><br>
             <select id="cmbProduct" name="cmbProduct"> //changes is in this code   
                    <option> -- Select -- </option>
                    <option value="Blackberries">Blackberries</option>
                </select>
                &ensp;@&ensp;
                <input type="number" id="txtQuantity" name="txtQuantity" value="1"
                       step="1" min="1" max="50" oninput="displayTotalTax()"><br><br>
            </fieldset>
            <br><br>

            <!-- Button to submit the form to PHP after validation -->
            <input type="submit" id="btnSubmit" value="Submit">
        </form>

在index.php中写道:

<?php
if(isset($_POST) && !empty($_POST)){
    echo "<pre/>";print_r($_POST);die;
}
?>

输出: -

Array
(
    [txtFirstName] => a
    [txtLastName] => b
    [txtAddress] => c
    [txtCity] => d
    [cmbProvince] => qc
    [txtPostalCode] => e
    [cmbProduct] => Blackberries
    [txtQuantity] => 1
)

答案 1 :(得分:2)

肯德尔,你是:

  • 缺少您的 <select id="cmbProduct"> 的名称,以及
  • 您的 cmbProduct 选项也缺失值

输入“禁用选择”是一个很好的做法,因为人们将无法选择“选择”选项:)

您的代码

<select id="cmbProduct">
   <option> -- Select -- </option>
   <option>Blackberries</option>
</select>

正确的代码

<select id="cmbProduct" name="cmbProduct>
  <option value="select" disabled selected> -- Select -- </option>
  <option value="blackberries">Blackberries</option>
</select>