1

Notice : Undefined index in Select option tag

When i open this program on browser, the error "undefined index" appear in SELECT option (drop-down box). Help me friends

<label>Your location<span class="error">*</span>:</label>
    <select name="loca">
      <option label="Select"></option>
      <option <?php if($_POST["loca"] == "Andaman and Nicobar Islands") echo "selected"; ?>>Andaman and Nicobar Islands</option>
      <option <?php if($_POST["loca"] == "Andhra Pradesh") echo "selected"; ?>>Andhra Pradesh</option>
      <option <?php if($_POST["loca"] =="Arunachal Pradesh") echo "selected"; ?>>Arunachal Pradesh</option>
      <option <?php if($_POST["loca"] =="Assam") echo "selected"; ?>>Assam</option>
      <option <?php if($_POST["loca"] == "Bihar") echo "selected"; ?>>Bihar</option>
      <option <?php if($_POST["loca"] == "Chandigarh") echo "selected"; ?>>Chandigarh</option>
      <option <?php if($_POST["loca"] =="Chhattisgarh") echo "selected"; ?>>Chhattisgarh</option>
      <option <?php if($_POST["loca"] =="Dadra and Nagar Haveli") echo "selected"; ?>>Dadra and Nagar Haveli </option>
      <option <?php if($_POST["loca"] =="Daman and Diu") echo "selected"; ?>>Daman and Diu</option>
      <option <?php if($_POST["loca"] =="Goa") echo "selected"; ?>>Goa</option>
      <option <?php if($_POST["loca"] =="Gujarat") echo "selected"; ?>>Gujarat</option>
      <option <?php if($_POST["loca"] =="Haryana") echo "selected"; ?>>Haryana</option>
      <option <?php if($_POST["loca"] =="Himachal Pradesh") echo "selected"; ?>>Himachal Pradesh</option>
      <option <?php if($_POST["loca"] =="Jammu and Kashmir") echo "selected"; ?>>Jammu and Kashmir</option>
      <option <?php if($_POST["loca"] =="Jharkhand") echo "selected"; ?>>Jharkhand</option>
      <option <?php if($_POST["loca"] =="Karnataka") echo "selected"; ?>>Karnataka</option>
      <option <?php if($_POST["loca"] =="Kerala") echo "selected"; ?>>Kerala</option>
      <option <?php if($_POST["loca"] =="Lakshadweep") echo "selected"; ?>>Lakshadweep</option>
      <option <?php if($_POST["loca"] =="Madhya Pradesh") echo "selected"; ?>>Madhya Pradesh</option>
      <option <?php if($_POST["loca"] =="Maharashtra") echo "selected"; ?>>Maharashtra</option>
   </select>
    <span class="error"><?php echo $locationErr?></span> <br />
Rikesh
  • 26,156
  • 14
  • 79
  • 87
Geetha
  • 353
  • 3
  • 7
  • 14

3 Answers3

4

it's always better to add a isset check in such cases

better to write your conditions this way

if(isset($_POST["loca"]) && $_POST["loca"]  == "Andaman and Nicobar Islands") echo "selected"; 
Manish Goyal
  • 700
  • 1
  • 7
  • 17
0

because your variable $_POST["loca"] is not set at the first request.

do <?php if(isset($_POST["loca"]) && $_POST["loca"] == "...")

Furthermore you should specify a value attribute on each option-tag.

leuchtdiode
  • 477
  • 3
  • 12
0

$ _POST ['Loca'] exists only if you submit the form, if not, initialize $ _POST ['loca'] like this: $ _POST ['loca'] = "";

/* Initialize var */

$_POST["loca"]="";
$locationErr="";

<label>Your location<span class="error">*</span>:</label>
<select name="loca">
// OPTION
</select>