PHP Codigniter javascripe验证

时间:2017-05-09 07:00:53

标签: php jquery

这是视图页面。我想在验证完成后它将重定向到某个控制器函数..但是它不起作用我的意思是window.location.href无效。

这是视图页面。我想在验证完成后,它将重定向到某个控制器功能

< !DOCTYPE html>                   这是教师页面                                                                                 

学生插入页面

                                                           名称:-                                                            手机号码:-                                                            电子邮件ID:-                                                            地址:-                                                                             学校名称                                                           选择任意                                                      “>                                                                                                                                                                       

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

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

1 个答案:

答案 0 :(得分:0)

您可以使用location.assign

&#13;
&#13;
$(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;
    }
    
  })
})
&#13;
<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>
&#13;
&#13;
&#13;