admin管理员组文章数量:1418715
I have a function in my view(aval_p.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?
I have a function in my view(aval_p.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?
Share Improve this question asked Jul 13, 2011 at 8:53 Rafael Morais dos SantosRafael Morais dos Santos 2961 gold badge6 silver badges17 bronze badges3 Answers
Reset to default 2Well 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:
<?php
echo "<script>calculateTotal()</script>";
?>
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.
You can't , Javascript & php are not executing on the same context, see php execute in the server , it sort of pile your code & provide a html page that is sent to client (here the browser) along with javascript & css . after that the browser execute the javascripts code.
i suggest tou simply try to rewrite your function on php . it seems its doing the total wich is more simple to do using a loop lik For().
CakePHP has lot of similarities to ruby-on-rails, but it is very unfortunate that there is no decent way to fire JS functions via controller against xhr requests in cake as happens in rails. But here is how it would work for you:
var $ponents = array('RequestHandler');
function async_fetch(){
if($this->ResquestHandler->isAjax()) {
$this->autoRender = false; // <-EDIT
echo "<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 to call a Javascript function using CakephpStack Overflow
版权声明:本文标题:How to call a Javascript function using Cakephp - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745291048a2651796.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论