George Spyrou
George Spyrou

Reputation: 61

HTML Combobox value not set

Keep getting this error in regards to a HTML combobox which I am trying to retrieve the selected value from via a separate PHP script:

PHP Notice: Undefined index: Item condition

This is the HTML code for the combo box:

<div>
    <p class="text-center">Item Details</p>
    <p>Model name</p>
    <div class="form-group>
        <input class="form-control" type="text" id='SellModel' name="Camera" placeholder="Start typing for suggested products...">
    </div>
    <p>Item condition</p>
    <div class="form-group text-center">
         <select class="form-control">
            <option value="undefined">Like New</option>
            <option value="12" selected="">Excellent</option>
            <option value="13">Good</option>
            <option value="14">Well Used</option>
        </select>
    </div>
    <div class="form-group text-center">
        <button class="btn btn-primary" type="submit">GET QUOTE</button>
    </div>
</div>

Here is the PHP code that queries a database based on the value of the combobox in the above HTML:

if($_POST["Item condition"] == "Like New") {
        try{
            $conn = new mysqli("localhost", "*******", "********", "********") or die ("Connect failed: %s\n". $conn -> error);
            $model = $_POST["Model name"];
            $query = "SELECT ".$model.", ln_price FROM quotes;";
            $quoteQuery - mysqli_query($conn, $query);
            $quoteArray = array();
            if(mysqli_num_rows($quoteQuery) > 0) {
                while($row = mysqli_fetch_row($quoteQuery)) {
                    $quoteArray[] = $row;
                }

            }
            echo $quoteArray[1];
        } catch (Exception $e) {
            echo 'Caught exception: ',  $e->getMessage(), "\n";
        }
    }


elseif($_POST["Item condition"] == "Excellent") {
        try{
            $conn = new mysqli("localhost", "*********", "********", "********") or die ("Connect failed: %s\n". $conn -> error);
            $model = $_POST["Model name"];
            $query = "SELECT ".$model.", excellent_price FROM quotes;";
            $quoteQuery - mysqli_query($conn, $query);
            $quoteArray = array();
            if(mysqli_num_rows($quoteQuery) > 0) {
                while($row = mysqli_fetch_row($quoteQuery)) {
                    $quoteArray[] = $row;
                }
            }
            echo $quoteArray[1];
        } catch (Exception $e) {
            echo 'Caught exception: ',  $e->getMessage(), "\n";
        }
    }


elseif($_POST["Item condition"] == "Good") {
        try{
            $conn = new mysqli("localhost", "*******", "*********", "*********") or die ("Connect failed: %s\n". $conn -> error);
            $model = $_POST["Model name"];
            $query = "SELECT ".$model.", good_price FROM quotes;";
            $quoteQuery - mysqli_query($conn, $query);
            $quoteArray = array();
            if(mysqli_num_rows($quoteQuery) > 0) {
                while($row = mysqli_fetch_row($quoteQuery)) {
                    $quoteArray[] = $row;
                }
            }
            echo $quoteArray[1];
        } catch (Exception $e) {
            echo 'Caught exception: ',  $e->getMessage(), "\n";
        }
    }

elseif($_POST["Item condition"] == "Well Used") {
        try{
            $conn = new mysqli("localhost", "********", "*********", "*********") or die ("Connect failed: %s\n". $conn -> error);
            $model = $_POST["Model name"];
            $query = "SELECT ".$mode

Upvotes: 2

Views: 211

Answers (1)

Madhu Jayarama
Madhu Jayarama

Reputation: 443

<div class="form-group text-center">
     <select name="Item_condition" class="form-control">
        <option value="11">Like New</option>
        <option value="12" selected="">Excellent</option>
        <option value="13">Good</option>
        <option value="14">Well Used</option>
    </select>
</div>

Add name="Item_condition" to the select tag. Make the value of "Like new" to 11.

if($_POST["Item_condition"] == "11") {
 // "like new" 
  ...
}


elseif($_POST["Item_condition"] == "12") {
   // "excellent"   
   ...
}


elseif($_POST["Item_condition"] == "13") {
   // Good
   ...
}

elseif($_POST["Item_condition"] == "14") {
   // Well said  
   ...
}

The value specified in $_POST["Item_condition'] should be the value specified in html <option> tag

Upvotes: 1

Related Questions