adding javascript with php tags

jrthor2 06 Aug, 2010
I'm trying to add the below to the Javascript section of the Form Code, but when I view my form, I just get a blank page


function enterAdditionalData() {
<?
if ($errCond = $session->get('err_lector_lay_assistant_info', false, 
md5('chronoerror'))) {?>
     document.getElementById('lector_lay_assistant_text').style.display='block';
<?}?>


<?
if ($errCond = $session->get('err_usher_info', false, 
md5('chronoerror'))) {?>
     document.getElementById('usher_text').style.display='block';
<?}?>

<?
if ($errCond = $session->get('err_choir_info', false, 
md5('chronoerror'))) {?>
     document.getElementById('choir_text').style.display='block';
<?}?>

<?
if ($errCond = $session->get('err_nursery_info', false, 
md5('chronoerror'))) {?>
     document.getElementById('nursery_text').style.display='block';
<?}?>

<?
if ($errCond = $session->get('err_altar_guild_info', false, 
md5('chronoerror'))) {?>
     document.getElementById('altar_guild_text').style.display='block';
<?}?>
}

My form is not even calling this function anywhere yet. if I view the source of the page, it says this:

function enterAdditionalData() {
<br />
<b>Fatal error</b>: Call to a member function get() on a non-object in <b>/home1/zluthorg/public_html/components/com_chronocontact/chronocontact.html.php(142) : eval()'d code</b> on line <b>21</b><br />

How can I get this to work? What I'm trying to do here, is, I have a form that has checkboxes, and when they are checked, it displays a textbox for soe additional info to be entered. I have the validation set up so that if there is an error on the page, and a checkbox is checked, then the textbox should be displayed that corresponds to the checkbox they checked. The validation works fine, I just can't get the textbox to show up when an error occurs.

Thanks
GreyHead 07 Aug, 2010
Hi jrthor2,

The immediate cause of the error is that $session isn't defined.
<?php
$session = JSession::getInstance('none', array());
?>
should fix this.

There are some other problems if ( a = b ) { will always return 'true'. I think you need if ( a == b ) { but even then $errCode isn't defined so I'm not clear what you are checking.

Also this code will only execute when the form is loaded so I'm not clear how it will interact with the JavaScript in the form itself or when the session values are set.

Bob
jrthor2 09 Aug, 2010
ok, I got the code to show up now when I view the source, but it is not populating the textboxes that are hidden by default with the value entered when a validation error occurs. Any help to get that working is greatly appreciated. The form is at http://www.zluth.org/stewardship-commitment-card-form. Also, if you check a time in the 3rd fieldset, and you get the textbox to show, and you put a value in, and then uncheck the checkbox, the textbox is hidden, but then if you click the time again, the value you typed in the textbox is gone.

Thanks
jrthor2 16 Aug, 2010
any help with this would be greatly appreciated!!!!
GreyHead 28 Aug, 2010
Hi jrthor2,

Sorry this got overlooked. I suspect that part of the problem is this line in the script which is clearing the entry when it is hidden.
document.getElementById(talent + '_info').value=''; 

I'm not certain that this is also causing the re-load problem but it may be. Try commenting it out and testing.

Bob
jrthor2 30 Aug, 2010
That didn't really help anything either. I think maybe the issue with if I have an error, and the text box that should be displayed if the user selected a time, may be that if I put javascript in the Form Javascript box when creating the form, this javascript gets loaded in the "head" of the html, but these text boxes aren't loaded till after that. I'm wondering if this javascript was down at the bottom, if this would work. Is it possible to put any javascript at the bottom of the form html?

Thanks
GreyHead 30 Aug, 2010
Hi jrthor2,

You can delay the extecution of a script until after the page is loaded by wrapping it in
window.addEvent('domready', function() {
  // script goes here
});

Bob
This topic is locked and no more replies can be posted.