php - 验证之后为什么我的选择标签数据被清除

时间:2014-01-27 10:49:13

标签: php html mysql css

我正在为我的html值进行PHP验证。但是,当PHP验证失败并返回页面时,将清除选择标记表单数据。无论如何都要在php中保存并重新加载表单数据

    <?php
$qualific=$passingyear="";
$qualificErr=$passingyearErr="";

if ($_SERVER['REQUEST_METHOD']== "POST") {
   $valid = true;
    //qualification validations starts here
     if(empty($_POST["qualif"]))
     {
         $qualificErr="* Qualification is Required";
         $valid=false;
     }
     else
     {
         $qualific=test_input($_POST["qualif"]);
     }
      //qualification validations starts here

      /*yearOfPassing validation starts here*/
       if(empty($_POST["yearpass"]))
     {
         $passingyearErr="* Year Of Pass is Required";
         $valid=false;
     }
     else
     {
         $passingyear=test_input($_POST["yearpass"]);
     }
      /*yearOfPassing validation starts here*/
     //if valid then redirect
  if($valid){
      include 'database.php';
      echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php">';    
    exit;
  }  
}

<form method="post" action="<?php htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label>Qualification<span class="error">*</span>:</label>
    <select name="qualif">
      <option label="Select"></option>
      <option>Below SSC(10 Std)</option>
      <option>SSC(10 Std) passed</option>
      <option>HSC(12 Std) passed</option>
      <option>Graduate</option>
      <option>Post Graduate</option>
    </select>
    <span class="error"><?php echo $qualificErr?></span> <br />
    <br />
    <label>Year of passing<span class="error">*</span>: </label>
    <select name="yearpass">
      <option label="Select"></option>
      <option>1975</option>
      <option>1976</option>
      <option>1977</option>
    </select>
</form>

4 个答案:

答案 0 :(得分:1)

在每个选项标记中尝试<?php if($_POST["qualif"] == "<value>") echo "selected"; ?>

喜欢

<option <?php if($_POST["qualif"] == "Below SSC(10 Std)") echo "selected"; ?>>Below SSC(10 Std)</option>

答案 1 :(得分:0)

目前您正在使用<option>而没有值。

使用类似:

<option value="SSC" <?php if(isset($_POST['qualif']) && $_POST['qualif'] == 'SSC') { echo "selected"; } ?> >SSC</option>

答案 2 :(得分:0)

  1. 您在value内缺少<option>参数。
  2. 您需要在<option>标记
  3. 中添加一些代码

    请尝试以下代码,希望这可以解决问题。

    <select name="qualif">
      <option label="Select"></option>
      <option value="Below SSC(10 Std)" <?php if($qualific == 'Below SSC(10 Std)') {?> selected <?php } ?>>Below SSC(10 Std)</option>
      <option value=">SSC(10 Std) passed" <?php if($qualific == '>SSC(10 Std) passed') {?> selected <?php } ?>>SSC(10 Std) passed</option>
      <option value="HSC(12 Std) passed" <?php if($qualific == 'HSC(12 Std) passed') {?> selected <?php } ?>>HSC(12 Std) passed</option>
      <option value="Graduate" <?php if($qualific == 'Graduate') {?> selected <?php } ?>>Graduate</option>
      <option value="Post Graduate" <?php if($qualific == 'Post Graduate') {?> selected <?php } ?>>Post Graduate</option>
    </select>
    <span class="error"><?php echo $qualificErr?></span> <br />
    <br />
    <label>Year of passing<span class="error">*</span>: </label>
    <select name="yearpass">
      <option label="Select"></option>
      <option value="1975" <?php if($passingyear == '1975') {?> selected <?php } ?>>1975</option>
      <option value="1976"  <?php if($passingyear == '1976') {?> selected <?php } ?>>1976</option>
      <option value="1977" <?php if($passingyear == '1977') {?> selected <?php } ?>>1977</option>
    </select>
    

答案 3 :(得分:0)

你应该使用jquery

<script type="text/javascript">
$(document).ready(function()
{
  $("#id").val("<?php echo $_POST['qualif']; ?>");  
});
</script>