简体   繁体   中英

Access value of selected option in <select> element with PHP

I am trying to create a simple form that will send data to a PHP script upon submitting the form. I am able to access all of the textbox values, and have successfully implemented variables for each of them. However, when I try to access the value of a combo box on my form, I get this error in the 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 (lines 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'];

Am I typing something wrong? Or missing something? Everything except the combo box is working.

Let me know if I need to include anything else. Thanks!


EDIT

Here is what happens when I try 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" }

Your code works for me with a little bit changes, try this:

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

In your index.php write:

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

Output:-

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

Kendall, you were:

  • missing name for your <select id="cmbProduct"> , and
  • missing value for your cmbProduct options as well

Putting in 'disabled selected' is a good practice as well as people won't be able to select the 'select' option :)

Your code

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

Correct code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM