表单使用 Ajax 在代码点火器中提交

form submit in codeigniter using ajax

本文关键字:点火器 提交 代码 Ajax 表单      更新时间:2023-09-26

当我提交表单并将检查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" );
                }
        }
    });
}

你不能在js文件中使用<?php echo $GLOBALS['base_url']; ?>。然后,将其包含在您的视图中可能会起作用。而不是<?php echo $GLOBALS['base_url']; ?>在您的视图中使用<?=base_url()?>

如果你的 js 函数kickerLogin()在 js 文件中,你不能使用 '',

调用函数时将 url 作为一个参数传递kickerLogin()

不建议在 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');
}