hello all
how to pass variable from zf to javascript/jquery?
thanks
hello all
how to pass variable from zf to javascript/jquery?
thanks
<?php
$this->view->foo = $foo;
?>
In your view:
<script type="text/javascript">
var foo = <?=Zend_Json::encode($this->foo)?>;
</script>
<?php
class SomeController extends Zend_Controller_Action {
public function indexAction() {
// a file
$this->view->headScript()->appendFile('yourJsFile.js');
// inline script
$message = 'hello!';
$this->view->headScript()->appendScript("jQuery(function(){alert('$message');});");
}
}
This requires that you add echo $this->headScript()
to your view- or layout-script.
If you want pass a variable to javascript function on any click event then here is the solution:
// link
<a href="#" onclick=myfunc( $this->myvalue ) >My Link</a>
// submit button
<input type=submit onclick=myfunc( $this->value ) />
<script type='text/javascript'>
function myfunc( value ) {
alert( value );
}
</script>
you can create your javascript dynamicly by useing Headscript View Helper it had function like :
<?php $this->headScript()->captureStart() ?>
var action = '<?php echo $this->baseUrl ?>';
$('foo_form').action = action;
<?php $this->headScript()->captureEnd() ?>
Source : ZF documentation