I have a function in my view(aval_comp.ctp).
When I do a calculation in is function on the controller, I want to call my Javascript function.
Js Function:
<script type="text/javascript">
function calculateTotal(){
var total = document.getElementById('valor1').innerHTML*1 + document.getElementById('valor2').innerHTML*1 + document.getElementById('valor3').innerHTML*1 + document.getElementById('valor4').innerHTML*1 + document.getElementById('valor5').innerHTML*1 + document.getElementById('valor6').innerHTML*1;
document.getElementById('total').innerHTML = total;
}
</script>
How do I call that function in the Controller?
Well talelcool is not quite right.
You can call javascript functions from php but it is highly discouraged and not how you should use the functions. PHP is server, JS is client, never mix the two!
Anyway, if in the php you echo out this:
That should work. It’s not perfect but if you put it at the end of the .php page it should call it.
The reason it might not work is it MAY try to call the function before the browser has had time to define it. If your are having that problem let me know 🙂
BUT
As previously stated, you should do this on the server. If you want to use values on the screen that have just been input, you can use AJAX. I suggest learning jQuery for this, as it will help you in alot of other areas as well.