簡體   English   中英

選擇選項下拉菜單隱藏輸入驗證

[英]select option dropdown hidden input validation

在下面的代碼中:我有一個select選項下拉列表(Employed,UmEmployed)如果我選擇Employed Option,則如果我提交數據時未插入任何內容,則顯示某些輸入字段(Current Designation,Current CTC),這表明驗證:需要當前指定。

如果我從下拉菜單中選擇另一個失業的選項,則應直接重定向到另一頁,但不會轉到也正在驗證的success.php(失業)。

<html> 
<head>  
<style>
#employer
{
    display:none;
}
.error
{
    color:#F00;
}
</style> 
<?php
$currentdes="";
$currentdesErr="";

if ($_SERVER['REQUEST_METHOD']== "POST") {
   $valid = true;
   if(empty($_POST["desig"]))
    {
        $currentdesErr="* Current Designation is Required";
        $valid=false;
        echo "<style>#employer{display:block;}</style>";
    }
    else
    {
        $currentdes=test_input($_POST["desig"]);
    }
    //if valid then redirect
  if($valid){
      include 'database.php';
      echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php">';    
    exit;
  }  
}

function test_input($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}

?>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
</head>
<body>
<form id="jsform" method="post" action="<?php htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p>Chose Your Browser: <select name = "currentsta" required>
        <option value = "">-- Select an Option --</option>
        <option value = "1" <?php if(isset($_POST["currentsta"]) && $_POST["currentsta"] == "1") echo "selected"; ?>>Employed</option>
        <option value = "2" <?php if(isset($_POST["currentsta"]) && $_POST["currentsta"] == "2") echo "selected"; ?>>UnEmployed</option>
        </select>
    </p>
  <div id="employer">
   Current Designation: <label><input type="text" name="desig" size="50" /></label>
    <span class="error"><?php echo $currentdesErr?></span>
    <br>
       Current CTC: <label><input type="text" size="50" /></label><br>

    </div>

<!--currentstatus starts here-->
<script type="text/javascript">

$('p select[name=currentsta]').change(function(e){
  if ($('p select[name=currentsta]').val() == '1'){
    $('#employer').show();
  }else{
    $('#employer').hide();
  }
});

</script>
<!--currentstatus Ends here-->

  <!--Submit Button-->
   <input type="button" value = "Submit" onClick=" document.getElementById('jsform').submit();" />
  </form>
</body>
</html>

由於未填寫,失業選項的設計輸入字段為空。 因此,您應該同時考慮這兩個條件。 也不要在if塊中包括database.php。 而是在success.php里面

<html> 
    <head>  
    <style>
    #employer
    {
        display:none;
    }
    .error
    {
        color:#F00;
    }
    </style> 
    <?php
    $currentdes="";
    $currentdesErr="";

    if ($_SERVER['REQUEST_METHOD']== "POST") {

       $valid = true;  
       if(empty($_POST["desig"]) && $_POST["currentsta"]==1)
        {
            $currentdesErr="* Current Designation is Required";
            $valid=false;
            echo "<style>#employer{display:block;}</style>";
        }
        else
        {
            $currentdes=test_input($_POST["desig"]);
        }
        //if valid then redirect
      if($valid){
          //include 'database.php';
          header('Location:success.php');   
        exit;
      }  
    }

    function test_input($data)
    {
         $data = trim($data);
         $data = stripslashes($data);
         $data = htmlspecialchars($data);
         return $data;
    }

    ?>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
    </script>
    </head>
    <body>
    <form id="jsform" method="post" action="<?php htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <p>Chose Your Browser: <select name = "currentsta" required>
            <option value = "">-- Select an Option --</option>
            <option value = "1" <?php if(isset($_POST["currentsta"]) && $_POST["currentsta"] == "1") echo "selected"; ?>>Employed</option>
            <option value = "2" <?php if(isset($_POST["currentsta"]) && $_POST["currentsta"] == "2") echo "selected"; ?>>UnEmployed</option>
            </select>
        </p>
      <div id="employer">
       Current Designation: <label><input type="text" name="desig" size="50" /></label>
        <span class="error"><?php echo $currentdesErr?></span>
        <br>
           Current CTC: <label><input type="text" size="50" /></label><br>

        </div>

    <!--currentstatus starts here-->
    <script type="text/javascript">

    $('p select[name=currentsta]').change(function(e){
      if ($('p select[name=currentsta]').val() == '1'){
        $('#employer').show();
      }else{
        $('#employer').hide();
      }
    });

    </script>
    <!--currentstatus Ends here-->

      <!--Submit Button-->
       <input type="button" value = "Submit" onClick=" document.getElementById('jsform').submit();" />
      </form>
    </body>
    </html>

如果選擇了失業選項,則要重定向到另一個頁面,然后使用JavaScript對其進行重定向。

在頁面末尾的腳本中,修改為-

$('p select[name=currentsta]').change(function(e){
  if ($('p select[name=currentsta]').val() == '1'){
    $('#employer').show();
  }else{
    //$('#employer').hide();
    //This redirects to next page on selecting the option.
    window.location.href= "success.php";
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM