Php 选择选项下拉菜单隐藏输入验证

Php 选择选项下拉菜单隐藏输入验证,php,html,Php,Html,在下面的代码中:我有一个选择选项dropdownEmployed,UmEmployed如果我选择了选项Employed,它会显示一些输入字段Current Designation,Current CTC如果我提交数据而不插入任何内容,它会显示validation:需要当前指定 如果我从下拉列表中选择了另一个未找到的选项,它应该直接重定向到另一个页面,但它不会成功。该选项也正在验证 <html> <head> <style> #employer {

在下面的代码中:我有一个选择选项dropdownEmployed,UmEmployed如果我选择了选项Employed,它会显示一些输入字段Current Designation,Current CTC如果我提交数据而不插入任何内容,它会显示validation:需要当前指定

如果我从下拉列表中选择了另一个未找到的选项,它应该直接重定向到另一个页面,但它不会成功。该选项也正在验证

<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>

如果在选择undeposed选项时,您希望重定向到另一个页面,然后使用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";
  }
});

失业选项的设计输入字段将为空,因为它未被填充。因此,你应该同时考虑这两个条件。也不要在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>

请尝试设置标题…@jycr753我可以知道如何设置标题吗header@jycr753我试着将代码重定向到另一个页面,但没有帮助friend@jycr753请尝试上面的代码,然后回答我的朋友。那你就知道我有什么问题了