14wml
14wml

Reputation: 4166

Select input is empty

People can select their graduation year from a selection input. I email their selection to myself. However, based on the fact that nothing is printing to the console, the $_POST["year"] must be empty, which is why no year is actually being sent to me as shown in the pic below. How do I fix this?

enter image description here

<div class = "mailInput"> Year: 
       <div class = "select" name = "year"><select>
             <option value="2017">2017</option>
             <option value="2018">2018</option>
             <option value="2019">2019</option>
              <option value="2020">2020</option>
        </select> 
       </div>
</div>

// Assign year input to year variable
if (!empty($_POST["year"])) {
     $year = $_POST["year"];
      echo("<script>console.log('Year after: ".$year."');</script>");
} 

Upvotes: 3

Views: 93

Answers (2)

Birhan Nega
Birhan Nega

Reputation: 681

    <div class = "mailInput"> Year: 
           <div class = "select" >
            //give name for select tag not for div
             //
                    <select name = "year">
                 <option value="2017">2017</option>
                 <option value="2018">2018</option>
                 <option value="2019">2019</option>
                  <option value="2020">2020</option>
            </select> 
           </div>
    </div>

// Assign year input to year variable
if (!empty($_POST["year"])) {
     $year = $_POST["year"];
      echo("<script>console.log('Year after: ".$year."');</script>");
}

Upvotes: 0

Rahul
Rahul

Reputation: 18557

You have written wrong html code,

<div class="mailInput"> Year: 
       <div ><select class="select" name="year"> // this line
             <option value="2017">2017</option>
             <option value="2018">2018</option>
             <option value="2019">2019</option>
              <option value="2020">2020</option>
        </select> 
       </div>
</div>

You have given name to div element which was parent of select, now above I gave name to select element. Give it a try. It will work.

Upvotes: 1

Related Questions