6

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?

2
  • yeah it's ok. Php parse at server side so before sending response it will parse all it's variable to strings and send the js code. Also, put them in <script> tag Commented May 16, 2016 at 5:47
  • if you okay with javascript then use Angular Js don't use codeigniter it will make your work heard. Commented May 16, 2016 at 7:30

2 Answers 2

5

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);
            }
        });
    });
   });
Sign up to request clarification or add additional context in comments.

Comments

3

Yes it is ok. For Eg:

$.ajax
            ({
                type:"POST",
                url:"<?php echo base_url(); ?>staff_activity/date_report",
                data:'year='+nep_year+'&month='+nep_month,
                success:function(msg)
                {

                    $('#result').html(msg);
                },
                beforeSend:function(d)
                {
                    $('#result').html("<center style='color:red;'>Please Wait......</center>");
                }
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.