2016-04-28 43 views
1
<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#fun").focusout(function() { 
      // alert("Value: " + $("#fun").val()); 
      $.post("<?php echo base_url() ?>index.php/login/email_exists", { 
       email: $("#fun").val(), 
      }, function(data, status) { 
       if (data == 0) { 
        document.getElementById("error").innerHTML = "Email already exists, Please select another email"; 
        document.getElementById("ddfun").focus(); 
        // alert("email already exists " + data); 
       } else { 
        document.getElementById("error").innerHTML = ""; 
       } 
       //alert("Data: " + data); 
      }); 
     }); 
    }); 
</script> 

我在我的应用程序中使用了更多的jQuery和Ajax。我总是在view.php文件中写入Ajax(带有php代码)。如果有任何选项可以在外部js文件中添加Ajax。任何帮助,将不胜感激。如何把ajax代码放在php codeigniter的external.js文件中

回答

1

你的问题是,你要插入一些PHP代码的输出到JS,这不会在.js文件中工作。

为了解决这个问题,您可以将该PHP输出作为属性放置在元素本身上,并在外部JS文件中将其读回。试试这个:

<!-- in the head --> 
<script src="/jquery.js"></script> 
<script src="/my-js-code.js"></script> 

<!-- example HTML element, the only relevant part is the 'data-base-url' attribute --> 
<div id="fun" data-base-url="<?php echo base_url() ?>"></div> 
// in my-js-code.js 
$(document).ready(function() { 
    $("#fun").focusout(function() { 
     $.post($(this).data('base-url') + "index.php/login/email_exists", { 
      email: $("#fun").val(), 
     }, function(data, status) { 
      if (data == 0) { 
       $("#error").html("Email already exists, Please select another email"); 
       $("#ddfun").focus(); 
      } else { 
       $("#error").html(''); 
      } 
     }); 
    }); 
}); 
+0

<脚本类型= “文/ JavaScript的”> $(函数(){$ 阿贾克斯({ 网址:'<?php echo base_url()?> task/get_sent_sms', type:'POST', data:{ client_id:<?php echo $ client-> client_id?>, project_id:<?php echo $ C lient-> project_id?> }, success:function(response){$('#sms_message')。val(''); //清空值字段 // alert(response); $('#sent_sms')。html(response); } }); }); 这个怎么样 – Rakesh

+0

这在外部js文件中不起作用。 –

+0

任何其他解决方案 – Rakesh

0

定义主布局文件JS变量,你在外部JS

打电话给你的外部JS

<script type = "text/javascript"> 
    var base_url = '<?php echo base_url() ?>'; 
</script> 

之前现在

$(document).ready(function() { 
$("#fun").focusout(function() { 
    // alert("Value: " + $("#fun").val()); 
    $.post(base_url + "index.php/login/email_exists", { 
     email: $("#fun").val(), 
    }, 
    function(data, status) { 
     if (data == 0) { 
     document.getElementById("error").innerHTML = "Email already exists, Please select another email"; 
     document.getElementById("ddfun").focus(); 
     // alert("email already exists " + data); 

     } else { 
     document.getElementById("error").innerHTML = ""; 
     } 
     //alert("Data: " + data); 
    }); 
}); 
0

当然可以。

你view.php

<script type = "text/javascript"> 
    var base_url = '<?php echo base_url() ?>'; 
</script> 
<script src="path/to/external.js"></script> 

您的外部。js

$(document).ready(function() { 
    $("#fun").focusout(function() { 
    // alert("Value: " + $("#fun").val()); 
    $.post(BASE_URL+"index.php/login/email_exists", { 
     email: $("#fun").val(), 
     }, 
     function(data, status) { 
     if (data == 0) { 
      document.getElementById("error").innerHTML = "Email already exists, Please select another email"; 
      document.getElementById("ddfun").focus(); 
      // alert("email already exists " + data); 

     } else { 
      document.getElementById("error").innerHTML = ""; 
     } 
     //alert("Data: " + data); 
     }); 
    }); 
}); 
+0

不是一个更好的方法 – Rakesh