value =“<! - ?php echo htmlspecialchars($ _ POST ['whatever']);? - >”不在select标签中工作

时间:2014-03-06 19:37:32

标签: php html5

我的代码写在下面

 <select name="year"  required  value="<?php echo htmlspecialchars($_POST['year']); ?>"  >
               <option>----SELECT----</option>
    <option value="1">1<sup>st</sup></option>
    <option value="2">2<sup>nd</sup></option>
    <option value="3">3<sup>rd</sup></option>
    <option value="4">4<sup>th</sup></option>
  </select>

htmlspecialchars()不适用于select tag.is有任何错误或有其他方法可以做到这一点

3 个答案:

答案 0 :(得分:0)

您没有为value标记分配select属性;您将其分配给option标记。目前尚不清楚您要做什么,但<select ... value="...">只是无效的HTML。 See the specification

答案 1 :(得分:0)

你不能在<select>标签中给出价值。值在<option>标记中给出。

<select name="year">
    <option>----SELECT----</option>
    <option value="<?php echo htmlspecialchars($_POST['year']); ?>">
     <?php echo htmlspecialchars($_POST['year']); ?></option>

    <option value="1">1<sup>st</sup></option>
    <option value="2">2<sup>nd</sup></option>
    <option value="3">3</sup>rd<sup></b></option>
    <option value="4">4<sup>rth</sup></option>
 </select>

答案 2 :(得分:0)

如果要设置选择的默认值,则必须将包含此值的选项设置为如下所示:

<select name="year" required="required">
    <option>----SELECT----</option>
    <option <?php if((int)($_POST['year'])==1){?>selected<?php } ?> value="1">1<sup>st</sup></option>
    <option <?php if((int)($_POST['year'])==2){?>selected<?php } ?> value="2">2<sup>nd</sup></option>
    <option <?php if((int)($_POST['year'])==3){?>selected<?php } ?> value="3">3</sup>rd<sup></b></option>
    <option <?php if((int)($_POST['year'])==4){?>selected<?php } ?> value="4">4<sup>rth</sup></option>
 </select>
相关问题