注意:选择选项标记
中的未定义索引
当我在浏览器上打开此程序时,SELECT选项(下拉框)中出现错误“undefined index”。 帮帮我朋友
<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 />
答案 0 :(得分:3)
在这种情况下添加isset检查总是更好
最好以这种方式写下你的条件
if(isset($_POST["loca"]) && $_POST["loca"] == "Andaman and Nicobar Islands") echo "selected";
答案 1 :(得分:0)
因为你的变量$ _POST [“loca”]没有在第一次请求时设置。
做<?php if(isset($_POST["loca"]) && $_POST["loca"] == "...")
此外,您应在每个选项标签上指定一个值属性。
答案 2 :(得分:0)
$ _POST [&#39; Loca&#39;]仅在您提交表单时才存在,如果没有,请按以下方式初始化$ _POST [&#39; loca&#39;]:$ _POST [&#39; loca& #39;] =&#34;&#34 ;;
/* Initialize var */
$_POST["loca"]="";
$locationErr="";
<label>Your location<span class="error">*</span>:</label>
<select name="loca">
// OPTION
</select>