2017-05-09 58 views
0

這是查看頁面。我想在驗證完成後重定向到某個控制器函數..但它不工作我的意思是window.location.href不工作。PHP Codigniter javascripe驗證

這是查看頁面。我想在驗證完成後會重定向到一個特定的控制器功能

<!DOCTYPE HTML> 這是老師頁

學生插入頁

名稱: - 手機號: - 電子郵件ID: - 地址: - 學校名稱 選擇任何 「>


          
  
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
    \t <script type="text/javascript"> 
 

 
    \t $(document).ready(function() 
 
    \t { 
 
    \t \t //validation(); 
 
    \t }); 
 
    \t function validation() 
 
    \t { 
 
    \t \t var flag=0; 
 
    \t \t var a=$("#stname").val(); 
 
    \t \t //alert(a); 
 
    \t \t if(a=="") 
 
    \t \t { 
 
    \t \t \t flag++; 
 
    \t \t \t $("#errName").html('Please Enter Name'); 
 
    \t \t \t $("#errName").css('color', 'red'); 
 
    \t \t } 
 
    \t \t else 
 
    \t \t { 
 
    \t \t \t $("#errName").html(''); 
 
    \t \t } 
 
    \t \t var b=$("#smob").val(); 
 
    \t \t if(b=="") 
 
    \t \t { 
 
    \t \t \t flag++; 
 
    \t \t \t $("#errMob").html('Please Enter Mobile'); 
 
    \t \t \t $("#errMob").css('color', 'red'); 
 
    \t \t } 
 
    \t \t else if(isNaN(b)) 
 
    \t \t { 
 
    \t \t \t flag++; 
 
    \t \t \t $("#errMob").html('Please Enter Numeric Mobile No'); 
 
    \t \t \t $("#errMob").css('color', 'red'); 
 
    \t \t } 
 
    \t \t else 
 
    \t \t { 
 
    \t \t \t $("#errMob").html(''); 
 
    \t \t } 
 
    \t \t var c=$("#semail").val(); 
 
    \t \t if(c=="") 
 
    \t \t { 
 
    \t \t \t flag++; 
 
    \t \t \t $("#errEmail").html('Please Enter Email'); 
 
    \t \t \t $("#errEmail").css('color', 'red'); 
 
    \t \t } 
 
    \t \t else 
 
    \t \t { 
 
    \t \t \t $("#errEmail").html(''); 
 
    \t \t } 
 
    \t \t var d=$("#sadd").val(); 
 
    \t \t if(d=="") 
 
    \t \t { 
 
    \t \t \t flag++; 
 
    \t \t \t $("#errAdd").html('Please Enter Address'); 
 
    \t \t \t $("#errAdd").css('color', 'red'); 
 
    \t \t } 
 
    \t \t else 
 
    \t \t { 
 
    \t \t \t $("#errAdd").html(''); 
 
    \t \t } 
 
    \t \t var e=$("#sname").val(); 
 
    \t \t if(e=="") 
 
    \t \t { 
 
    \t \t \t flag++; 
 
    \t \t \t $("#errSchool").html('Please Choose School'); 
 
    \t \t \t $("#errSchool").css('color', 'red'); 
 
    \t \t } 
 
    \t \t else 
 
    \t \t { 
 
    \t \t \t $("#errSchool").html(''); 
 
    \t \t } 
 
    \t \t //alert(flag); 
 
    \t \t if(flag>0) 
 
    \t \t { 
 
    \t \t \t return false; 
 
    \t \t } 
 
    \t \t else 
 
    \t \t {   \t \t \t window.location.href="http://127.0.0.1/CI/index.php/admin/school_controller/student_insert"; 
 
    \t \t } 
 
    \t } 
 
    </script>
<!DOCTYPE html> 
 
    <html> 
 
    <head> 
 
    \t <title>This is Teacher Page</title> 
 
    </head> 
 
    <body> 
 
    <center> 
 
    <form method="post" action="" onsubmit="return validation()"> 
 
    <?php echo validation_errors();?> 
 
    \t <table border="1"> 
 
    \t \t <tr> 
 
    \t \t \t <td colspan="2" align="center"> 
 
    \t \t \t <h1>Student Insert Page</h1> 
 
    \t \t \t </td> 
 
    \t \t </tr> 
 
    \t \t <tr> 
 
    \t \t \t <td>Name:-</td> 
 
    \t \t \t <td><input type="text" name="stname" id="stname"><span id='errName'></span></td> 
 
    \t \t </tr> 
 
    \t \t <tr> 
 
    \t \t \t <td>Mobile No:-</td> 
 
    \t \t \t <td><input type="text" name="stmob" id="smob"><span id='errMob'></span></td> 
 
    \t \t </tr> 
 
    \t \t <tr> 
 
    \t \t \t <td>Email Id:-</td> 
 
    \t \t \t <td><input type="text" name="stemail" id="semail"><span id='errEmail'></span></td> 
 
    \t \t </tr> 
 
    \t \t <tr> 
 
    \t \t \t <td>Address:-</td> 
 
    \t \t \t <td><input type="text" name="stadd" id="sadd"><span id='errAdd'></span></td> 
 
    \t \t </tr> 
 
    \t \t \t <input type="hidden" name="user_level" value="S"> 
 
    \t \t <tr> 
 
    \t \t \t <td>School Name</td> 
 
    \t \t \t <td> 
 
    \t \t \t \t <select name="sname" id="sname"> 
 
    \t \t \t \t <option value="">Select Any</option> 
 
    \t \t \t \t <?php 
 
    \t \t \t \t \t if(isset($school)) 
 
    \t \t \t \t \t { 
 
    \t \t \t \t \t \t foreach ($school as $value) { 
 
    \t \t \t \t \t \t \t ?> 
 
    \t \t \t \t \t \t \t <option value="<?php echo $value['id'];?>"><?php echo $value['name'];?></option> 
 
    \t \t \t \t \t \t \t <?php 
 
    \t \t \t \t \t \t } 
 
    \t \t \t \t \t } 
 
    \t \t \t \t ?> 
 
    \t \t </select><span id='errSchool'></span> 
 
    \t \t \t </td> 
 
    \t \t </tr> 
 
    \t \t <tr> 
 
    \t \t \t <td colspan="2" align="center"><input type="submit" name="submit" value="Insert" ></td> 
 
    \t \t </tr> 
 
    \t </table> 
 
    </form> 
 
    </center> 
 
    </body> 
 
    </html>
+0

請看到我的最新答案 –

+0

你說的 「不工作」 的意思,它是什麼在做什麼? – gabe3886

+0

我再次更新了我的答案,希望它能幫助你。 –

回答

0

可以使用location.assign

$(function(){ 
 

 
    $("#btnSubmit").click(function(e){ 
 
    e.preventDefault(); 
 
    var _form = $(this).closest("form"); 
 
    //validate jquery 
 
    var validator = _form.validate(); 
 
    
 
    var errorList = []; 
 
    
 
    _form.find("input").each(function(){ 
 
     if ($(this).attr("type") != "submit"){ 
 
     if (!validator.element(this)) 
 
     { 
 
      errorList.push($(this).attr("name") + " - has error"); 
 
     } 
 
     else if ($(this).val() == "") 
 
     { 
 
      errorList.push($(this).attr("name") + " - needs value"); 
 
     } 
 
     }  
 
    }) 
 
    
 
    if (errorList.length == 0) 
 
    { 
 
     window.location.assign("http://www.w3schools.com"); 
 

 
    } 
 
    else 
 
    { 
 
     
 
     console.log(errorList) 
 
     //alert(validator.valid()); 
 
     return false; 
 
    } 
 
    
 
    }) 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script> 
 

 
<form id="form1"> 
 
    <input type="email" name="email" /> 
 
    <input type="text" name="firstname" /> 
 
    <input type="text" name="lastname" /> 
 
    <input type="submit" id="btnSubmit"/> 
 
</form>

+0

沒有我想使用jquery/JavaScript的 –

+0

<表格ID = 「form1的」 方法= 「POST」 行動= 「」>做驗證 如果驗證是真的,將重定向與window.location.href =「特定控制器」 –

+0

更新我的答案。 –