Codigniter架構使用jquery+ajax/" target="_blank">jquery ajax代碼如下:
| 代碼如下 |
複製代碼 |
<form action="<?php echo @htmlspecialchars($url['login_url']) ?>" method="post" enctype="application/x-www-form-urlencoded" name="form1" id="form1"> <p><label>工資號:</label> <input name="Login.Token1" class="text" type="text" id="token1" /> </p> <p><label>密 碼:</label> <input name="Login.Token2" class="text" type="password" id="token2" /> </p> <p> <input name="登入" type="submit" id="user_login" value="登入" /> <input type="reset" value="重設" /> </p> <b style="color:red"><?php echo $this->session->flashdata('error'); ?></b> </form> <script> $(function(){ $("#user_login").click(function(){ var username = $("#token1").val(); var salary_no = $("#token2").val(); $.ajax({ type: "POST", data: "username="+username+"&salary_no="+salary_no, url: "<?php echo site_url('home/ajax_check_username')?>", dataType: "text", cache: false, error: function(){alert('error');}, success: function(data){ if(data == 'yes'){ location.href="<?php echo site_url('home/index')?>"; }else{ form1.submit(); } } }); return false; }); }); </script> |
ajax提交到home控制器代碼:
| 代碼如下 |
複製代碼 |
#提交到本地 function ajax_check_username(){ //redirect('home/index'); $username = $this->input->post("username"); $salary_no = $this->input->post("salary_no"); if($this->user_mdl->check_ajax_user($username,$salary_no)){ $this->session->set_userdata('salary_no', $salary_no); $this->session->set_userdata('username', $username); echo "yes"; }else{ echo "no"; } }
|