sudhakar phad sudhakar phad - 5 months ago 12
jQuery Question

is it ok to write Javascript or jQuery code as a PHP file in CodeIgniter views?

I have created a JavaScript file as a PHP file in my CodeIgniter views so that I can access some of my PHP dynamic variable in JavaScript.

For example:

$.ajax({
url: "<?php echo base_url('loginpage'); ?>",
type: 'POST',
data: {emailId:email1,password:pwd1},
success: function(result){


Is this OK?

Answer

It is perfectly alright , I am using this in a professional project

 var base_url = '<?php echo base_url(); ?>';
 $(document).ready(function(){

    $('#searchCategory').change(function(){
        var id = $('#searchCategory').val();
        $.ajax({
            type: "POST",
            url: base_url+"home/getSubCategory",
            data: {id:id},
            success: function(data) {
                $("#sub_category").html(data);
            }
        });
    });
   });
Comments