Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 852
sudhakar phad Avatar asked May 16 '16 05:05

sudhakar phad


People also ask

Can we write jQuery in PHP?

Use <script> to Use jQuery in PHP As with any JavaScript file, which jQuery is, we need the <script> tag to include it.

Can a PHP file contain JS?

JavaScript is used as client side to check and verify client details and PHP is server side used to interact with database. In PHP, HTML is used as a string in the code. In order to render it to the browser, we produce JavaScript code as a string in the PHP code.

How load JavaScript file in CodeIgniter?

Adding JavaScript and CSS (Cascading Style Sheet) file in CodeIgniter is very simple. You have to create JS and CSS folder in root directory and copy all the . js files in JS folder and . css files in CSS folder as shown in the figure.


1 Answers

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);
            }
        });
    });
   });
like image 198
Malik Mudassar Avatar answered Sep 20 '22 06:09

Malik Mudassar