Jump to content

[solved] Problem With Javascript In An *.inc File


Naex
 Share

Recommended Posts

Hello, I am trying to add functionality to an existing program, and the way the last guy did it was to have one master page that loads whichever form is needed. I'm trying to use javascript to lock/unlock a checkbox if another one is activated. My javascript program (called directworkplace.js) is:

function workclick(bool) {		var direct = getElementById('direct');		if (bool)				direct.checked=false;				direct.enabled=false;		}		else {				direct.enabled=true;		}}

the php form:

function form_add_edit_mileage($reportid,$mileageid=0){...<div align="center"><!---<script type="text/javascript" src="directworkplace.js">---!><form method="POST" action="<?echo $PHP_SELF?>">...		<td align="right"  class="tcaption">Direct Project Cost? <?echo displayReqChar()?></td>		<td class="tdata"><input type="checkbox" id="direct" name="direct" <? if( $qdata->direct ) echo 'checked="true"'; ?>  /></td></tr><tr>		<td align="right"  class="tcaption">First Place of Work? <?echo displayReqChar()?></td>		<td class="tdata"><input type="checkbox" name="placework" onClick="workclick(this.value)" /></td></tr>...</form>

I hope I've made my problem clear. I'm very new to javascript and think I am just missing something, although neither my coworker or I can figure it out.

Edited by Naex
Link to comment
Share on other sites

enabled is not a valid property, try:

function workclick(bool) {		var direct = getElementById('direct');		if (bool)				direct.checked=false;				direct.disabled=true;		}		else {				direct.disabled=flase;		}}

also according to the src element of your script tag your external js file is located in the same folder as the master template page. If this is not the case the path is wrong.Are you getting any errors?

Link to comment
Share on other sites

They're both in the same folder. I've also found another problem that's related, when I include the "<script type="text/javascript" src="directworkplace.js">" line in the code, in IE the form doesn't appear (you only see the side panels, etc. nothing from the form).

Edited by Naex
Link to comment
Share on other sites

Yes, although since he fragmented it so much there's going to be many different bits. The directories are not a problem, they're all correct.directworkplace.js

function workclick(bool) {		var direct = getElementById('direct');		if (bool)				direct.checked=false;				direct.disabled=true;		}		else {				direct.disabled=flase;		}}

expense_common.inc

function form_add_edit_mileage($reportid,$mileageid=0){		global $PHP_SELF;		global $formBorderColor;		global $formContentColor;		global $Sdept;		include ("include/config.inc");		$db = connectToDB($dbinfo);		$date = mktime(0,0,0,date('m'),date('d'),date('Y'));		$d = '';		$m = '';		$y = '';		$text = "Add";		$inputVal = "Add Mileage";		$department = $Sdept;		$noblank = 1;		if ($mileageid){				$qdata = get_mileage_entry($mileageid);				$text = "Edit";				$inputVal = "Save Mileage";				$date = $qdata->date;				$m = date('m',$date);				$d = date('d',$date);				$y = date('Y',$date);				$department = $qdata->deptnumber;				$msg = "				This is a previously saved mileage expense. Make the				necessary updates and save it";		}		$dept = new Department($db,$department);?><div align="center"><form method="POST" action="<?echo $PHP_SELF?>"><input type="hidden" name="do_action_mileage" value="save and list entries"><input type="hidden" name="id" value="<?echo $mileageid?>"><input type="hidden" name="reportid" value="<?echo $reportid?>"><table width="70%" cellpadding="1" cellspacing="0" border="0" bgcolor="<?echo $formBorderColor?>"><tr><td><table width="100%" cellpadding="3" cellspacing="0" border="0" bgcolor="<?echo $formContentColor?>"><tr>		<td align="left" class="thead" colspan="2"><? bullet() ?>  		<? echo $text ?> Mileage		</td></tr><tr>		<td align="left" class="tdata" colspan="2">		Fields marked with * are required.<br>		Wondering how many miles from place A to place B? See the		<a href="mileagehelp.html" onclick="window.open('mileagehelp.html','new','width=570,height=400,top=20,right=20,scrollbars=yes,resizable=yes');return$		</td></tr><tr>		<td align="right"  class="tcaption">Date: </td>		<td span class="tdata">		<? echo get_months_select($m,'month',$noblank) ?>  		<? echo get_days_select($d,'day',$noblank) ?>  		<? echo get_years_select($y,'year',1,1,$noblank) ?>  		</td></tr><tr>		<td align="right"  class="tcaption">Department:</td>		<td class="tdata"><? echo $dept->getDepartmentSelectBox("deptnumber",$department,1) ?></td></tr><tr>		<?		$origin = ($qdata->origin)? $qdata->origin:"Office (Round Trip)";		?>		<td align="right"  class="tcaption">Origin:</td>		<td  class="tdata"> <? echo $origin ?>		<input type="hidden" name="origin" size="30" maxlength="60" value="<? echo $origin?>"></td></tr><tr>		<td align="right"  class="tcaption">Destination: <?echo displayReqChar()?></td>		<td class="tdata"><input type="text" name="destination" size="30" maxlength="60" value="<?echo $qdata->destination?>"></td></tr><tr>		<td align="right"  class="tcaption">Description:</td>		<td class="tdata"><input type="text" name="description" size="30" maxlength="60" value="<?echo $qdata->description ?>"></td></tr><tr>		<td align="right"  class="tcaption">Kms: <?echo displayReqChar()?></td>		<td class="tdata"><input type="text" name="kms" size="5" maxlength="5" value="<?echo $qdata->kms?>"></td></tr><tr>		<td align="right"  class="tcaption">Direct Project Cost? <?echo displayReqChar()?></td>		<td class="tdata"><input type="checkbox" id="direct" name="direct" <? if( $qdata->direct ) echo 'checked="true"'; ?>  /></td></tr><tr>		<td align="right"  class="tcaption">First Place of Work? <?echo displayReqChar()?></td>		<td class="tdata"><input type="checkbox" name="placework" onClick="workclick(this.value)" /></td></tr><tr>		<td align="center" colspan="2" class="tdata">				<input type="submit" name="do_action_mileage" value="Save and List Entries">  				<input type="submit" name="do_action_mileage" value="Save and Add New Entry">  </tr><tr>		<td align="center" colspan="2" class="tdata">				<input type="submit" name="do_action_mileage" value="Save and Finish">  				<input type="submit" name="do_action_mileage" value="Cancel">  		</td></tr></table></td></tr></table></table></form></div><?}//end of form_add_edit_mileage()

I can't copy thereport.php (the primary page), but I can tell you essentially what it does. Every time the page loads it needs an action, which is sent through a get statement, that tells it to load the proper form (the one in question is above) from expense_common.inc. Its HTML header is likewise tucked away inside another php call, which makes it difficult to add the javascript call in there.

Edited by Naex
Link to comment
Share on other sites

That fixed the disappearing page problem, but unfortunately the javascript is still not doing anything.

function form_add_edit_mileage($reportid,$mileageid=0){		global $PHP_SELF;		global $formBorderColor;		global $formContentColor;		global $Sdept;		include ("include/config.inc");		$db = connectToDB($dbinfo);		$date = mktime(0,0,0,date('m'),date('d'),date('Y'));		$d = '';		$m = '';		$y = '';		$text = "Add";		$inputVal = "Add Mileage";		$department = $Sdept;		$noblank = 1;		if ($mileageid){				$qdata = get_mileage_entry($mileageid);				$text = "Edit";				$inputVal = "Save Mileage";				$date = $qdata->date;				$m = date('m',$date);				$d = date('d',$date);				$y = date('Y',$date);				$department = $qdata->deptnumber;				$msg = "				This is a previously saved mileage expense. Make the				necessary updates and save it";		}		$dept = new Department($db,$department);?><script type="text/javascript" src="directworkplace.js"></script><div align="center"><form method="POST" action="<?echo $PHP_SELF?>"><input type="hidden" name="do_action_mileage" value="save and list entries"><input type="hidden" name="id" value="<?echo $mileageid?>"><input type="hidden" name="reportid" value="<?echo $reportid?>"><table width="70%" cellpadding="1" cellspacing="0" border="0" bgcolor="<?echo $formBorderColor?>"><tr><td><table width="100%" cellpadding="3" cellspacing="0" border="0" bgcolor="<?echo $formContentColor?>"><tr>		<td align="left" class="thead" colspan="2"><? bullet() ?>  		<? echo $text ?> Mileage		</td></tr><tr>		<td align="left" class="tdata" colspan="2">		Fields marked with * are required.<br>		Wondering how many miles from place A to place B? See the		<a href="mileagehelp.html" onclick="window.open('mileagehelp.html','new','width=570,height=400,top=20,right=20,scrollbars=yes,resizable=yes');return$		</td></tr><tr>		<td align="right"  class="tcaption">Date: </td>		<td span class="tdata">		<? echo get_months_select($m,'month',$noblank) ?>  		<? echo get_days_select($d,'day',$noblank) ?>  		<? echo get_years_select($y,'year',1,1,$noblank) ?>  		</td></tr><tr>		<td align="right"  class="tcaption">Department:</td>		<td class="tdata"><? echo $dept->getDepartmentSelectBox("deptnumber",$department,1) ?></td></tr><tr>		<?		$origin = ($qdata->origin)? $qdata->origin:"Office (Round Trip)";		?>		<td align="right"  class="tcaption">Origin:</td>		<td  class="tdata"> <? echo $origin ?>		<input type="hidden" name="origin" size="30" maxlength="60" value="<? echo $origin?>"></td></tr><tr>		<td align="right"  class="tcaption">Destination: <?echo displayReqChar()?></td>		<td class="tdata"><input type="text" name="destination" size="30" maxlength="60" value="<?echo $qdata->destination?>"></td></tr><tr>		<td align="right"  class="tcaption">Description:</td>		<td class="tdata"><input type="text" name="description" size="30" maxlength="60" value="<?echo $qdata->description ?>"></td></tr><tr>		<td align="right"  class="tcaption">Kms: <?echo displayReqChar()?></td>		<td class="tdata"><input type="text" name="kms" size="5" maxlength="5" value="<?echo $qdata->kms?>"></td></tr><tr>		<td align="right"  class="tcaption">Direct Project Cost? <?echo displayReqChar()?></td>		<td class="tdata"><input type="checkbox" id="direct" name="direct" <? if( $qdata->direct ) echo 'checked="true"'; ?>  /></td></tr><tr>		<td align="right"  class="tcaption">First Place of Work? <?echo displayReqChar()?></td>		<td class="tdata"><input type="checkbox" name="placework" onClick="workclick(this.value)" /></td></tr><tr>		<td align="center" colspan="2" class="tdata">				<input type="submit" name="do_action_mileage" value="Save and List Entries">  				<input type="submit" name="do_action_mileage" value="Save and Add New Entry">  </tr><tr>		<td align="center" colspan="2" class="tdata">				<input type="submit" name="do_action_mileage" value="Save and Finish">  				<input type="submit" name="do_action_mileage" value="Cancel">  		</td></tr></table></td></tr></table></table></form></div><?}//end of form_add_edit_mileage()

Link to comment
Share on other sites

I tried it on a much smaller scale:

<html><body><script type="text/javascript">function workclick(bool) {		var direct = getElementById('direct');		if (bool)				direct.checked=false;				direct.disabled=true;		}		else {				direct.disabled=flase;		}}</script>Direct Project Cost?<input type="checkbox" id="direct" /><br />First Place of Work?<input type="checkbox" name="placework" onClick="workclick(this.value)" /></body></html>

And this doesn't work either. The onClick event isn't working.

Edited by Naex
Link to comment
Share on other sites

You're missing an opening bracket after the if statement, use a debugger to help catch errors like that. Also, use document.getElementById. You also misspelled "false" once, and send this.checked to the function instead of this.value.

Edited by justsomeguy
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...