Retrieval of php variable in a jquery function

Hi,

Can anyone please tell me how to retrieve a php variable into a jquery function. Both are found in the same file. Here is the code:

<div class="form">

<?php Yii::app()->getClientScript()->registerScript(‘myScript’,’$("#dol").change(function()

{

var A = parseInt(document.getElementById(&quot;dol&quot;).value);


var myVar = &lt;?php echo json_encode(&#036;vat); ?&gt;; (This does not work)


document.getElementById(&quot;dol2&quot;).value=A+myVar;





}


);');?&gt;

<?php $form=$this->beginWidget(‘CActiveForm’, array(

'id'=&gt;'product-form',





'enableAjaxValidation'=&gt;false,

)); ?>

&lt;p class=&quot;note&quot;&gt;Fields with &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; are required.&lt;/p&gt;


&lt;?php echo &#036;form-&gt;errorSummary(&#036;model); ?&gt;





&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'description'); ?&gt;


&lt;?php echo &#036;form-&gt;textField(&#036;model,'description',array('size'=&gt;60,'maxlength'=&gt;100)); ?&gt;


&lt;?php echo &#036;form-&gt;error(&#036;model,'description'); ?&gt;&lt;br&gt;





&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'dol'); ?&gt;


&lt;?php echo &#036;form-&gt;textField(&#036;model,'dol',array('size'=&gt;10,'id'=&gt;'dol')); ?&gt;


&lt;?php &#036;vat = &#036;this-&gt;totals('VAT'); ?&gt;


&lt;?php echo &#036;form-&gt;error(&#036;model,'dol'); ?&gt;
  1. Well technically you’re trying to echo $vat before assigning a value to it. It should be the opposite :wink:

  2. You’re putting PHP into PHP code!

Given those 2 remarks, try with




…

<?php $vat = $this->totals('VAT'); ?>

…

<?php Yii::app()->getClientScript()->registerScript("myScript", "$('#dol').change(function()

{

var A = parseInt(document.getElementById('dol').value);

var myVar = " . json_encode($vat) . ";

document.getElementById('dol2').value=A+myVar;

}

);");?>

Note: I inverted quotes and double quotes above for convenience.

If you want to be sure, replace your [font="Courier New"]json_encode($vat)[/font] by a integer-containing string to check your jQuery/PHP mixed code.

  1. Other remark: you’re (Yii is) already using jQuery. So why not using it also for selectors?



…

<?php $vat = $this->totals('VAT'); ?>

…

<?php Yii::app()->getClientScript()->registerScript("myScript", "$('#dol').change(function()

{

var A = parseInt($('#dol').val());

var myVar = " . json_encode($vat) . ";

$('#dol2').val(A+myVar);

}

);");?>

PS And please, use formatting when you post your code…

PPS What was the error output with your non-working code? Or was it a blank page?

Edit: corrected two erroneous semicolons in my suggestions

The function is supposed to update a field(dol2) in the form by the sum of dol+vat. Well nothing happens: dol2 remains unchanged.

I modified the code as per your suggestion, it still does not work: I get a concatenation of the text value of dol followed by '.json_encode($vat). Thanks for your other suggestion. It works fine. I am quite new to the whole thing and the problem is that I have a project to work upon. So I am learning the rough way. I am learning while working :(

Hi there. I don’t understand. Is it working or not working? :blink:

Can you copy-paste the code you have that is not working, after modification?

Hi,

I am sorry for the confusion. It is still not working. Here is the code after modification:

[color="#FFA500"][font="Microsoft Sans Serif"]

<?php $vat = $this->totals(‘VAT’); ?>

<div class="form">

<?php Yii::app()->getClientScript()->registerScript(‘myScript’,’$("#dol").change(function()

{

var A = parseInt($("#dol").val());

var myVar = " . json_encode($vat) . ";(This does not work)

$("#dol2").val(A+myVar);

}

);’);?>

<?php $form=$this->beginWidget(‘CActiveForm’, array(

‘id’=>‘product-form’,

‘enableAjaxValidation’=>false,

)); ?>

<p class="note">Fields with <span class="required">*</span> are required.</p>

<?php echo $form->errorSummary($model); ?>

<?php echo $form->labelEx($model,‘description’); ?>

<?php echo $form->textField($model,‘description’,array(‘size’=>60,‘maxlength’=>100)); ?>

<?php echo $form->error($model,‘description’); ?><br>

<?php echo $form->labelEx($model,‘dol’); ?>

<?php echo $form->textField($model,‘dol’,array(‘size’=>10,‘id’=>‘dol’)); ?>

<?php echo $form->error($model,‘dol’); ?>

<?php echo $form->labelEx($model,‘dol2’); ?>

<?php echo $form->textField($model,‘dol2’,array(‘size’=>10,‘id’=>‘dol2’)); ?>

<?php echo $form->error($model,‘dol2’); ?>

[/font] [/color]

Here is what is rendered after modification in field dol2:-

[color="#FF0000"][font="Times New Roman"]

1180 . json_encode($vat) . [/font] [/color]

I assigned a value to the variable $vat at the beginning though I am not sure this could be an issue here since the function is called on the event change and at this time a value had already been assigned to the variable. Am I wrong?

Well you haven’t used my code as it was.

Forget that. Given your code now, You need to change this line


var myVar = " . json_encode($vat) . ";

into


var myVar = ' . json_encode($vat) . ';

PS After reviewing your code, I wonder what you’re trying to achieve…

Hi Bennouna,

Thank you very much. I think I was so worked up at this that I missed to invert the quotes for this line. It works like a charm (I added the parseInt function). What I was trying to achieve (and which I achieved now thanks to your precious help) is updating all the values that are dependent on this first value and the value of $this->totals(‘VAT’) I have got many other fields in the form that depend on these 2 values. There are also many other pairs of values like this in the form that determine other fields.

PS. I guess I’ll be needing your help again in the future (if you are willing of course :D ) Like I said, I am learning while working… and it’s quite hard at times. Thanks again

Hello BIanca. Glad it helped.

On this forum, everyone is willing to help, and I’m sure that everyone is also learning for fun or on purpose.

I think someone once said that you stop learning only when you die.