0
I am using codeigniter (based on PHP and Model-Control-View). I have a controller that loads a Model and a View.
[Controller]
public function index()
{
$data['agenda_model'] = $this->load->model('agenda_model');
$this->load->view('home', $data);
$this->load->view('commons/bootstrap-scripts');
}
[View]
<body onload="initialize()">
</body>
<script>
function initialize(){
console.log(<?php echo $agenda_model->getAgendaDate('05-03-2019'); ?>);
}
</script>
[Model]
public function getAgendaDate($date){
echo $date;
$sql = 'select * from agenda';
$result = $this->db->query($sql);
return $result->num_rows;
}
When I run the above code, I have the following error in the script. ERROR: Uncaught Referenceerror: initialize is not defined at onload
[Simplification/Debbuging] To simplify things, I decided to do a php echo inside javascript.
var result = <?php echo "maria"; ?>;
console.log(result);
(index):2698 Uncaught ReferenceError: maria is not defined
[update] - 02/03/2019
"PHP runs next to the server" this may explain why I can’t perform one of the Model functions. Is there a Pattern design to run model methods in the view? However this does not explain why it does not perform:
var result = <?php echo "maria"; ?>;
My question. It will be efficient with alternative, do HTTP GET/POST?
Thanks for the help. Where I run the functions to instantiate the core?
– Pedro Correia
In the file itself where you want to use it. In your case, in the view. Remembering that this is a sin in MVC haha
– Paulo César Moraes
It also seems to go against the ideology of MVC. For example if you make a $_GET('name') in the view, which means the 'name' argument. Where do I wait for GET? Do I have to assign a route? Runs a little away from the topic, but it’s an alternative solution. However not yet found clear documentation for GET/POST, can help me?
– Pedro Correia
Considering that you are using version 3.x of the IC, you can see the documentation that explains about get, post and everything else related to inputs here: https://www.codeigniter.com/user_guide/libraries/input.html
– Paulo César Moraes