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?
Use <script> to Use jQuery in PHP As with any JavaScript file, which jQuery is, we need the <script> tag to include it.
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.
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.
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);
}
});
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With