This topic is locked

PHPR 5.2 enable/disable field based on checkbox

1/26/2010 12:50:05 PM
PHPRunner General questions
A
aalekizoglou author

Hi,
In PHPR 5.2 this topic (Enabled/Disabled based on another field) does not work. I have tried the following:



function BeforeShowAdd(&$xt,&$templatefile)

{
$body=$xt->xt_vars["body"];
$body["end"].= <<<EOT

<script type=text/javascript>

document.forms.editform.value_bMoveable.onchange=function()

{

if(this.options[this.selectedIndex].checked)

{

document.forms.editform.value_iDays.disabled=false;

addValid.add(document.editform['value_iDays'],'IsRequired','State: ');

}

else

{

document.forms.editform.value_iDays.disabled=true;

document.forms.editform.value_iDays.value='';

undefine('value_iDays');

}

}

function undefine(n)

{

if (document.editform != null)

{

for (i = 0; i < addValid.checkObjects.length; i++)

{

if (addValid.checkObjects[i][0].name==n)

{

var elem = addValid.checkObjects[i][0];

var par = $(elem).parent();

$('div',par).remove('.error');

addValid.checkObjects.splice(i,1);

}

}

}

}
</SCRIPT>
EOT;
$xt->assignbyref("body",$body);
;

} // function BeforeShowAdd


The code does not work anymore. In fact I tried to put a buggy code like this

<script type=text/javascript>

buggy code to raise a javascript error

</SCRIPT>


to see a firebug error, and I found that the script is not running at all.
Regards

S
steveh 1/26/2010

5.2 it looks more like this:-
$body=$xt->xt_vars["body"];

$body['end']['object']->addJSCode('your javascript code...');

A
aalekizoglou author 1/26/2010

Thanks Steve,
I just changed that and got a
Fatal error: Call to a member function addJSCode() on a non-object in C:\.....
Any hint?

S
steveh 1/26/2010

Add code to var_dump $body to a file. Check on firephp if you're using firebug.

A
aalekizoglou author 1/27/2010



Add code to var_dump $body to a file. Check on firephp if you're using firebug.


Steve,
here is the dump of $body.



array(

['begin'] =>

'

<script type="text/javascript">

window.debugMode = false;

</script><script language="JavaScript" src="include/jquery.js"></script>

<script language="JavaScript" src="include/jsfunctions.js"></script>

<script type="text/javascript" src="include/runnerJS/RunnerBase.js"></script><script language="JavaScript" src="include/jsfunctions.js"></script>

<div id="search_suggest"></div>

...........

...........

['end'] =>

'</form><script>Runner.util.ScriptLoader.addPostLoadStep(function()

{

window.TEXT_MONTH_JAN='Ιανουάριος';

window.TEXT_MONTH_FEB='Φεβρουάριος';

window.TEXT_MONTH_MAR='Μάρτιος';

window.TEXT_MONTH_APR='Απρίλιος';

window.TEXT_MONTH_MAY='Μάιος';

window.TEXT_MONTH_JUN='Ιούνιος';

.........

.........

........

..........

Runner.util.ScriptLoader.addJS(['customlabels']);

Runner.util.ScriptLoader.addJS(['ajaxsuggest']);

Runner.util.ScriptLoader.addJS(['json']);

Runner.util.ScriptLoader.addJS(['runnerJS/RunnerControls']);

Runner.util.ScriptLoader.load(1);</script>'

...

)


In my previous attempt with assigning the $body to the $xt I get this dump



array(

['begin'] =>

'

<script type="text/javascript">

window.debugMode = false;

</script><script language="JavaScript" src="include/jquery.js"></script>

<script language="JavaScript" src="include/jsfunctions.js"></script>

<script type="text/javascript" src="include/runnerJS/RunnerBase.js"></script><script language="JavaScript" src="include/jsfunctions.js"></script>

<div id="search_suggest"></div>

...........

...........

['end'] =>

'</form><script>Runner.util.ScriptLoader.addPostLoadStep(function()

{

window.TEXT_MONTH_JAN='Ιανουάριος';

window.TEXT_MONTH_FEB='Φεβρουάριος';

window.TEXT_MONTH_MAR='Μάρτιος';

window.TEXT_MONTH_APR='Απρίλιος';

window.TEXT_MONTH_MAY='Μάιος';

window.TEXT_MONTH_JUN='Ιούνιος';

.........

.........

........

..........

Runner.util.ScriptLoader.addJS(['customlabels']);

Runner.util.ScriptLoader.addJS(['ajaxsuggest']);

Runner.util.ScriptLoader.addJS(['json']);

Runner.util.ScriptLoader.addJS(['runnerJS/RunnerControls']);

Runner.util.ScriptLoader.load(1);</script>'

<script type=text/javascript>

document.forms.editform.value_bMoveable.onchange=function()

{

if(this.options[this.selectedIndex].checked)

{

document.forms.editform.value_iDays.disabled=false;

addValid.add(document.editform['value_iDays'],'IsRequired','State: ');

}

else

{

document.forms.editform.value_iDays.disabled=true;

document.forms.editform.value_iDays.value='';

undefine('value_iDays');

}

}

function undefine(n)

{

if (document.editform != null)

{

for (i = 0; i < addValid.checkObjects.length; i++)

{

if (addValid.checkObjects[i][0].name==n)

{

var elem = addValid.checkObjects[i][0];

var par = $(elem).parent();

$('div',par).remove('.error');

addValid.checkObjects.splice(i,1);

}

}

}

}
</SCRIPT>


where it show that I get the javascript in the body template but maybe in a wrong place?
Regards,

S
steveh 1/27/2010

In your example you can just use $body['end'].='blah blah';

A
aalekizoglou author 1/27/2010



In your example you can just use $body['end'].='blah blah';


Yes, I did so. That is how I have always done in the past, and that is how I got the script in place. The problem is that scripts that we insert at the end of the body do not run at all and I suspect that has something to do with PHPR 5.2 and the new way scripts are injected in the body and run.

S
steveh 1/27/2010

text/javascript should be surrounded by double quotes?
i.e. "text/javascript"
I'm using the addJSCode elsewhere in 5.2 to enable/disable inline edit so the method you use depends on the object you're passed.

J
Jane 1/28/2010

Athanasios,
we'll update all tips&tricks for the new version in the near future.