2014-01-31 105 views
0

在下面的代码:我有一个选择选项下拉菜单(就业,UmEmployed)如果我选择选项运用这显示了一些输入字段(当前型号,目前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> 
+0

尝试设置标题,而不是内部... – jycr753

+0

@ jycr753可我知道如何设置页眉 – user2978559

+0

http://dk1.php.net/manual/en/function.header。 PHP – jycr753

回答

1

的DESIG输入字段为空失业选择,因为它没有被填满。因此你应该考虑这两个条件。也不要在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> 
+0

你打我两秒钟..笑好一个 – jycr753

+0

@shanavascet感谢ü这么多朋友 – user2978559

0

如果选择失业选项,您希望重定向到另一页,然后使用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"; 
    } 
}); 
相关问题