passing php variable from controller to javascript function in the head of a view
yeah sure, just echo the javascript: <?php echo '<script>var myPhpVariable = "'. $my_var . '";</script>'; ?>
passing php variable from controller to javascript function
I have solved this issue, hope this is useful to someone in the future.
An alternative you could consider is adding the variable needed by the Javascript to some sort of HTML element in your view, and then use JS to pull that variable and then do whatever you need with it.
From the controller echo the value within a div id = 'demo'
and then from Javscript call.
var total_count = 0;
total_count = document.getElementById("demo").innerHTML;
document.getElementById("demo").innerHTML = '';
// this is to not display the value but to capture into a Javscript variable and use it and not display from echoed value from controller.
That's it!
How to pass variables from PHP to Javascript
In my opinion, the best way to pass many variables from PHP to Javascript is to collect them in a PHP object, convert the object to JSON and print it out in a script tag on your website.
In your controller:
$config = new stdClass();
$config->site_url = site_url('manage/getSerials');
$config->user_name = 'David';
...pass the $config
var somehow to your view.
In your view:
<script type="text/javascript">
var MyConfig = <?= json_encode($config); ?> // global var "MyConfig"
</script>
It will generate this code:
<script type="text/javascript">
var MyConfig = {
site_url: "what/your/function/returned",
user_name: "David"
}
</script>
And finally access your vars like this in your Javascript files:
console.log(MyConfig.site_url)
The global Javascript var MyConfig
has the additional benefit of having all your dynamic variables namespaced in a Javascript object.
Pass php variable from view to controller to model using codeigniter
Hope this will help you :
Put your variable subcategory_id
outside the ajax like this :
$(document).ready(function(e){
fillgrid();
function fillgrid()
{
var subcategory_id = '<?php echo $sub_id; ?>';
$("#loader").show();
$.ajax({
url:'<?=base_url("Shop/get_products_in_sub_subcategory/");?>'+subcategory_id,
type:'GET'
}).done(function (data){
alert(data);
console.log(data)
$("#fillgrid").html(data);
$("#loader").hide();
});
}
});
And your method get_products_in_sub_subcategory
should be like this :
public function get_products_in_sub_subcategory($subcategory_id)
{
echo $subcategory_id;
//$this->Account_model->get_products_in_sub_subcategory($subcategory_id);
exit;
}
Related Topics
Check Whether the String Is a Unix Timestamp
How to Convert Output of Number_Format Back to Numbers in PHP
Leverage Browser Caching for 3Rd Party Js
Php: Convert Milliseconds to Date
PHP Exec() Will Not Execute Shell Command When Executed via Browser
Find Out Which Class Called a Method in Another Class
How to Handle Error for Duplicate Entries
Remove Exif Data from Jpg Using PHP
How to Write Cdata Using Simplexmlelement
How to Convert Output of Number_Format Back to Numbers in PHP
How to Use Cookies Across Two Different Domains
PHP Random Shuffle Array Maintaining Key => Value
Fix Malformed Xml in PHP Before Processing Using Domdocument Functions