使用ajax在codeigniter中提交表单

时间:2013-09-30 07:10:51

标签: javascript php jquery ajax codeigniter

这里当我提交表单并将检查js文件中的验证然后将调用kickerLogin()函数 收到datastring的警告信息,然后这不会发送到我在ajax中提到的网址,但会提交..........

function kickerLogin(){
    alert('hello friends');
    dataString=$('form[name=kickerLog]').serialize();
    alert(dataString);
    $.ajax({
        type:"POST",
        url:"<?php echo $GLOBALS['base_url']; ?>ajax/user-ajax.php?mode=kickerLogin",
        cache: false,
        data: dataString,
        dataType: "json",

        success: function(data) {
            alert(data);
                if(data.success == "yes")
                {   
                    $('#kickerLog').submit();   
                }
                else if(data.success == "no")
                { 
                    if(data.status=="emailfail"){
                    $('li.log_error').show();
                     $('li.log_error').html("Email id not verified");
                    } 
                    else if(data.status=="rejected"){
                        alert("Your account is inactive by admin");
                    } 

                 else{  
                     $('li.log_error').show();
                     $('li.log_error').html("Invalid Email / Password");
                     $("#loginEmail").css("border","1px solid red");        
                     $("#loginPassword").css("border","1px solid red");    
                 }
                }

                else {
                    alert(" Occured internal Error.please check network connection" );
                }
        }
    });
}

3 个答案:

答案 0 :(得分:2)

您不能在js文件中使用<?php echo $GLOBALS['base_url']; ?>。在您的视图中包含此功能可能会起作用。 <?php echo $GLOBALS['base_url']; ?>在您的视图中使用<?=base_url()?>

答案 1 :(得分:0)

如果您的js函数kickerLogin()位于js文件中,则无法使用'',

在调用kickerLogin()函数

时将您的网址作为一个参数传递

答案 2 :(得分:0)

建议不要在CI中调用外部文件。

//Instead of this
 url:"<?php echo $GLOBALS['base_url']; ?>ajax/user-ajax.php?mode=kickerLogin",

//Use this
//Where ajax is a controller ajax.php user_ajax is a function in it.
 url:"<?php echo site_url();?>/ajax/user_ajax?mode=kickerLogin",

//ajax.php controller
function user_ajax(){
$data['mode'] = $this->input->get('mode');
//Here load the file
$this->load->view('user-ajax');
}