?
Solved

Javascript error states form name is not defined although it is

Posted on 2008-02-05
7
Medium Priority
?
1,920 Views
Last Modified: 2010-08-05
Sorry about the state of the code below I'm using dreamweaver and its crap! Anyway about half
way down the script there is a form named form2!

That form includes an object that displays a list of images and when I select one of the images in the list a preview appears. It worked until embedding it within a more complicated page and now I get a javascript error stating 'Form2' has not been defined.

That error comes from the script about 19 lines down. Any ideas why this is happening?
<?
session_start();
if(!session_is_registered(myusername)){
header("location:index.php");
}
?>
<?php include 'phptxt/header.inc'; ?>
<head>
<title>iFolio</title>
	<?php include 'phptxt/meta.inc'; ?>
	<link href="css/main.css" rel="stylesheet" type="text/css">
	<link href="css/user.css" rel="stylesheet" type="text/css">
<script language="JavaScript">
function change(html){
  descriptions.innerHTML=html
}
function pre() {
form2.preview.src = 'gallery/thumbnails/image'+form2.choiceimage.options[form2.choiceimage.selectedIndex].value+'.jpg';
}
</script>
</head>
 
<body>
 
<div id="Table_01">
<table id="Table_01" width="830" height="926" border="0" cellpadding="0" cellspacing="0">
		
	<tr><!-- Header START -->
	  <td width="830" height="153" colspan="3">
  	    <div id="LogID">
  	      <div align="center">You are logged in<br/>
  	        <img src="images/Me n Suit.jpg" width="75" height="82"><br/>
  	        <a href="/ifolio/loggedout.php">Log Out</a>  	      </div>
  	    </div>
	    <?php include 'phptxt/title.inc'; ?>
							<div id="TitleL">
			  <?php include 'phptxt/google1.inc'; ?>
	    </div>
	  </td>
	</tr><!-- Header END -->
	
	<tr>
		<td width="28" height="773" rowspan="3">
			<img src="images/spacer.gif" width="28" height="773" alt="">		</td>
		
		<td background="images/MIDDLE_03.gif" width="787" height="620"><!-- LOGIN BOX START -->
		  <!-- LOGIN BOX END -->
		  
	<!-- USER ACCOUNT MENU START -->
	<div id="menuposition" background="images/TOP_ContentBoxGreenLeft.gif" width="" height="200">    	
			<?php include 'phptxt/usermenu.inc'; ?><!-- Footer with User Award Details -->
	</div>
	<div id="descriptions" class="menutxt">Welcome Back <?php echo $myusername ?></div>
	<!-- USER ACCOUNT MENU END -->
	
	    <!-- MAIN CONTENT BOX 2 START -->
	<div id="MainContentBox2" background="images/TOP_MainContentBox.gif" width="749" height="284">
		<div>Upload New Images </div>
		<div>
	<?
if ($action=="save") {
if ($i=="") for ($i=1; file_exists("gallery/images/image".strval($i).".jpg"); $i++) {
}
$file ="gallery/des/image$i.txt";
$fp =fopen($file, "w+");
$name=stripslashes($name);
$des=stripslashes($des);
$code=stripslashes($code);
$author=stripslashes($author);
$w ="name=".$name."&price=".$price."&code=".$code."&des=".$des."&author=".$author."&mail=".$mail."&date=".$date."&web=".$web;
for ($ii=1; $ii<100; $ii++) {
$w=stripcslashes($w);
}
if ($down<>"") $w = $w."&counter=0";
fwrite($fp, $w);
fclose($fp);
if (copy($thumb, "gallery/thumbnails/image".strval($i).".jpg"))  echo "Thumbnail copied<br>";
if (copy($image, "gallery/images/image".strval($i).".jpg"))  echo "Image copied<br>";
if (copy($down, "gallery/down/image".strval($i).".zip"))  echo "Download File copied";
} else {
$file="gallery/des/image$i.txt";
///
if (file_exists("gallery/des/image$i.txt")) {
$fp=fopen($file, "r");
$w=fread($fp, filesize($file));
for ($j=1; $j<10; $j++) {
$w=stripcslashes($w);
}
parse_str($w);
$name=stripslashes($name);
$des=stripslashes($des);
$code=stripslashes($code);
$author=stripslashes($author);
fclose($fp);
}
?><form method="post" onSubmit="return validate();" enctype="multipart/form-data" name="form1" action="<?php echo $PHP_SELF;?>">
<p></p>
 <table width="725">
    <tr> 
  		<td colspan="2">		
		</td>
	<td width="146" rowspan="2"> 
		<div align="right"><font size="1" face="verdana">File Source:
		  
		  <input name="i" type="hidden" id="i" value=<?php echo $i;?>>
		  </font> </div></td>
	<td width="222" rowspan="2"><font size="1" face="verdana">
	  <input name="image" type="file" id="image" value='images/image<?php echo $i.'jpg';?>'>
	</font></td>
    </tr>
    <tr>
		<td width="192" height="41">
		  <div align="right"><font size="1" face="verdana"> Name:    	</font> </div></td>
		<td width="145"><font size="1" face="verdana">
		  <input name="name" type="text" id="name" value='<?php echo $name;?>'>
		</font></td>
    </tr>
    <tr>
		<td>		<div align="right"><font size="1" face="verdana">
		  Price :
		  
		  </font> </div></td>
		<td><font size="1" face="verdana">
		  <input name="price" type="text" id="price" value="<?php echo $price;?>">
		</font></td>
		<td width="146"><div align="right"><font size="1" face="verdana">Thumbnail:
		  
		  </font></div></td>
        <td width="222"><font size="1" face="verdana">
          <input name="thumb" type="file" id="thumb" value='thumbnails/image<?php echo $i.'jpg';?>'>
        </font></td>
    </tr>
    <tr>
		<td>
		  <div align="right"><font size="1" face="verdana">Date:
		    
		    <input name="action" type="hidden" id="action" value="save">
		          <input name="password" type="hidden" id="action" value="<?php echo $password;?>">
		      </font> </div></td>
		<td><font size="1" face="verdana">
		  <input name="date" type="text" id="date" value='<?php echo $date;?>'>
		</font></td>
		<td>
				<div align="right"><font size="1" face="verdana">Download File:    	</font> </div></td>
        <td><font size="1" face="verdana">
          <input name="down" type="file" id="down" value='down/image<?php echo $i.'jpg';?>'>
        </font></td>
    </tr>
    <tr>
		<td>
		  <div align="right"><font size="1" face="verdana">Author:</font></div></td>
		<td><input name="author" type="text" id="author" value='<?php echo $author;?>'></td>
		<td rowspan="3">
			<div align="right"><font size="1" face="verdana">Description:</font></div></td>
        <td rowspan="3"><font size="1" face="verdana">
          <textarea name="des" id="des"><?php echo $des;?></textarea>
        </font></td>
    </tr>
	    <tr>
		<td>
		  <div align="right"><font size="1" face="verdana">
		    Author Web:    	</font> </div></td>
		<td><font size="1" face="verdana">
		  <input name="web" type="text" id="web" value='<?php echo $web;?>'>
		</font></td>
	    </tr>
    <tr>
		<td>
		  <div align="right"><font size="1" face="verdana">E-mail:</font></div></td>
		<td><input name="mail" type="text" id="mail" value='<?php echo $mail;?>'></td>
    </tr>
	<tr>
		<td colspan="4"><div align="center"><font size="1" face="verdana">
		  <input type="submit" name="Submit" value="Submit">
		  </font></div></td>
		</tr>
</table>
</P>
</form>
<?php }?>
	</div>
	</div>
	<!-- MAIN CONTENT BOX 2 END -->
 
    <!-- MAIN CONTENT BOX START -->
	<div id="TOP-MainContentBox" background="images/TOP_MainContentBox.gif" width="749" height="284">
		<div>Edit Existing Images </div>
		<div>
		<?
if ($submit=="Edit") {
echo "<script language='JavaScript'>
window.location = 'USERIMAGES.php?i=$choiceimage&password=$password';
</script>";
}elseif ($submit=="Delete") {
for ($i=1; file_exists("gallery/images/image".strval($i).".jpg"); $i++) {
}
$num=$i-1;
function unlinkk($f) {
if (!file_exists($f)) return true;
else return unlink($f);
}
function del($i) {
if (unlinkk("gallery/images/image$i.jpg") and unlinkk("gallery/thumbnails/image$i.jpg") and unlinkk("gallery/down/image$i.zip")
and unlinkk("gallery/des/image$i.txt")) return true;
}
if (!del($choiceimage)) echo "Error";
for ($i=intval($choiceimage)+1; $i<$num; $i--) {
$j=$i-1;
if (!rename("gallery/images/image$i.jpg", "gallery/images/image$j.jpg")) $error="r";
}
if ($error=="") echo "Image$choiceimage deleted successfully";
} else {
echo "<form name=\"form2\" action=\"$PHP_SELF\" method=\"post\">
  <p align=\"center\"><font color=\"#CCCCCC\" size=\"1\" face=\"verdana\"><strong>Select Image<br>
    <table border=0><tr><td><select name=\"choiceimage\" size=7 onChange=\"pre()\">";
       for ($i=1; file_exists("gallery/thumbnails/image".strval($i).".jpg"); $i++) {
    echo "<option value=$i>Image$i</option>";
	}
    echo "</select></td>
    </strong></font></p>
  <p align=center>
    <td><img src=\"gallery/noth.gif\" border=0 name=preview></td></p></tr></table>
  <p align=\"center\"><strong><font color=\"#CCCCCC\" size=\"1\" face=\"verdana\">
    <input type=\"submit\" name=\"submit\" value=\"Edit\">
    <input type=\"hidden\" name=\"password\" value=\"$password\">
    <input type=\"submit\" name=\"submit\" value=\"Delete\">
    </font></strong> </p>
</form>";
}
 
?>
		</div>
	</div>
	<!-- MAIN CONTENT BOX END -->		
	
	</td>
		<td width="15" height="773" rowspan="3">
		<img src="images/spacer.gif" width="15" height="773" alt=""></td>
	</tr>
	
		
	<tr><!-- FOOTER START -->
		<td background="images/MIDDLE_05.gif" width="787" height="141">
			<?php include 'phptxt/footer.inc'; ?><!-- Footer with User Award Details -->		</td>
	</tr><!-- FOOTER END -->
	
	<tr><td width="787" height="12">
	<img src="images/spacer.gif" width="787" height="12" alt=""></td>
	</tr>
</table>
</div>
 
</body>
</html>

Open in new window

0
Comment
Question by:Ryan Bayne
7 Comments
 
LVL 5

Expert Comment

by:Koma666
ID: 20824887
try to add id="form2" to the form and please post the code of the html page from the browser not the php code, it's hard to read the php serverside code with all the variables. It would be easier if you just copy and paste the source code of the web page from your browser.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 20824963
If your "form2" is defined within a <table> element, I don't think that will work. But it is hard to tell, viewing your code :-(
0
 
LVL 2

Author Comment

by:Ryan Bayne
ID: 20827166
OK I've cleaned up the source code. Tried adding id="form2" but doesnt work.

I considered it being the fact that I use escape code for a form earlier on the page maybe!
<!-- Ryan Bayne -->
<!-- iFolio -->
<!-- All Rights Reserved 2008 -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>iFolio</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="title" content="iFolio By www.webtechglobal.co.uk">
<meta name="description" content="Student resource site">
<meta name="keywords" content=
"designer,web,design,internet,intranet,extranet,looking,for,a,graphics,graphic,logos,logo,low,price,quality,uni,university,scotland,england,uk,kirkcaldy,fife,dundee,perth,webtg,web,tech,technician,technical,global, flash, interactive,banners,ad,banner,page,contact,form,dynamic,static,ryan,zypher,optimization,optimisation,optimize,optimise,photo,photography,law,brunel,saima,khalid,downloads,recordings,lecture">
<link rel="icon" href="favicon.ico" type=
"image/vnd.microsoft.icon">
<link href="css/main.css" rel="stylesheet" type="text/css">
<link href="css/user.css" rel="stylesheet" type="text/css">
<script language="JavaScript" type="text/javascript">
function change(html){
  descriptions.innerHTML=html
}
function pre() {
form2.preview.src = 'gallery/thumbnails/image'+form2.choiceimage.options[form2.choiceimage.selectedIndex].value+'.jpg';
}
</script>
</head>
 
<body>
<div id="Table_01">
<table id="Table_01" width="830" height="926" border="0"
cellpadding="0" cellspacing="0">
<tr><!-- Header START -->
<td width="830" height="153" colspan="3">
<div id="LogID">
<div align="center">You are logged in<br>
<img src="images/Me%20n%20Suit.jpg" width="75" height="82" alt=
"#########"><br>
<a href="/ifolio/loggedout.php">Log Out</a></div>
</div>
<div id="Logo"><img src="images/logo.gif" width="298" height="117"
alt=""></div>
<div id="TitleL"><script type="text/javascript">
<!--
google_ad_client = "pub-4923567693678329";
//GoogleAds 125x125
google_ad_slot = "3166759704";
google_ad_width = 125;
google_ad_height = 125;
google_cpa_choice = ""; // on file
//-->
</script> <script type="text/javascript" src=
"http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
 
</td>
</tr>
<!-- Header END -->
<tr>
<td width="28" height="773" rowspan="3"><img src=
"images/spacer.gif" width="28" height="773" alt=""></td>
<td background="images/MIDDLE_03.gif" width="787" height="620">
<!-- LOGIN BOX START -->
<!-- LOGIN BOX END -->
<!-- USER ACCOUNT MENU START -->
<div id="menuposition" background=
"images/TOP_ContentBoxGreenLeft.gif" width="" height="200">
<ul id="usermenu">
<li><a href='Account.php' onmouseover=
"javascript:change('View and manage your personnel information from here')"
onmouseout=
"javascript:change('Remember to always Log-Out when finished')">Account</a></li>
<li><a href='Friends.php' onmouseover=
"javascript:change('Connect to friends interact with each others profiles')"
onmouseout=
"javascript:change('Please change your password regularly')">Friends</a></li>
<li><a href='Sales.php' onmouseover=
"javascript:change('View your sales information both purchases and sold items')"
onmouseout=
"javascript:change('You can use Paypal to buy and sell')">Sales</a></li>
 
<li><a href='Portfolio.php' onmouseover=
"javascript:change('Manage your portfolio and CV from here')"
onmouseout=
"javascript:change('Remember to update your portfolio')">Portfolio</a></li>
<li><a href='USERFILES.php' onmouseover=
"javascript:change('Upload, organise and set file settings in here')"
onmouseout=
"javascript:change('With premium services you can upload larger files')">
Files</a></li>
<li><a href='Mail.php' onmouseover=
"javascript:change('Send mail to other members or iFolio')"
onmouseout=
"javascript:change('Never send personal information to other users')">
Mail</a></li>
<li><a href='forum/index.php' onmouseover=
"javascript:change('Visit the user forum and discuss anythign you like')"
onmouseout=
"javascript:change('People can be very helpfull in the forum')">Forum</a></li>
<li><a href='USERCHAT.php' onmouseover=
"javascript:change('Chat with other members who are online now')"
onmouseout=
"javascript:change('Our chat service is very safe and secure')">Chat</a></li>
<li><a href='stats.php' onmouseover=
"javascript:change('View stats on profile views, log time, sales and much more')"
onmouseout=
"javascript:change('You can win things on the stats page')">My
Stats</a></li>
<li><a href='settings.php' onmouseover=
"javascript:change('Personnel settings to make iFolio work the way you want')"
onmouseout=
"javascript:change('Personalise your profile and shop')">Settings</a></li>
</ul>
 
<!-- Footer with User Award Details --></div>
<div id="descriptions" class="menutxt">Welcome Back test1</div>
<!-- USER ACCOUNT MENU END -->
<!-- MAIN CONTENT BOX 2 START -->
<div id="MainContentBox2" background=
"images/TOP_MainContentBox.gif" width="749" height="284">
<div>Upload New Images</div>
<div>
<form method="post" onsubmit="return validate();" enctype=
"multipart/form-data" name="form1" action="/ifolio/userimages.php">
<table width="725">
<tr>
<td colspan="2"></td>
<td width="146" rowspan="2">
<div align="right"><font size="1" face="verdana">File Source:
<input name="i" type="hidden" id="i" value=""></font></div>
</td>
 
<td width="222" rowspan="2"><font size="1" face=
"verdana"><input name="image" type="file" id="image" value=
'images/imagejpg'></font></td>
</tr>
<tr>
<td width="192" height="41">
<div align="right"><font size="1" face="verdana">Name:</font></div>
</td>
<td width="145"><font size="1" face="verdana"><input name="name"
type="text" id="name" value=''></font></td>
</tr>
<tr>
<td>
<div align="right"><font size="1" face="verdana">Price
:</font></div>
</td>
<td><font size="1" face="verdana"><input name="price" type="text"
id="price" value=""></font></td>
<td width="146">
<div align="right"><font size="1" face=
"verdana">Thumbnail:</font></div>
 
</td>
<td width="222"><font size="1" face="verdana"><input name="thumb"
type="file" id="thumb" value='thumbnails/imagejpg'></font></td>
</tr>
<tr>
<td>
<div align="right"><font size="1" face="verdana">Date: <input name=
"action" type="hidden" id="action" value="save"> <input name=
"password" type="hidden" id="action" value=""></font></div>
</td>
<td><font size="1" face="verdana"><input name="date" type="text"
id="date" value=''></font></td>
<td>
<div align="right"><font size="1" face="verdana">Download
File:</font></div>
</td>
<td><font size="1" face="verdana"><input name="down" type="file"
id="down" value='down/imagejpg'></font></td>
</tr>
<tr>
 
<td>
<div align="right"><font size="1" face=
"verdana">Author:</font></div>
</td>
<td><input name="author" type="text" id="author" value=''></td>
<td rowspan="3">
<div align="right"><font size="1" face=
"verdana">Description:</font></div>
</td>
<td rowspan="3"><font size="1" face="verdana">
<textarea name="des" id="des">
</textarea></font></td>
</tr>
<tr>
<td>
<div align="right"><font size="1" face="verdana">Author
Web:</font></div>
 
</td>
<td><font size="1" face="verdana"><input name="web" type="text" id=
"web" value=''></font></td>
</tr>
<tr>
<td>
<div align="right"><font size="1" face=
"verdana">E-mail:</font></div>
</td>
<td><input name="mail" type="text" id="mail" value=''></td>
</tr>
<tr>
<td colspan="4">
<div align="center"><font size="1" face="verdana"><input type=
"submit" name="Submit" value="Submit"></font></div>
</td>
</tr>
</table>
</form>
 
</div>
</div>
<!-- MAIN CONTENT BOX 2 END -->
<!-- MAIN CONTENT BOX START -->
<div id="TOP-MainContentBox" background=
"images/TOP_MainContentBox.gif" width="749" height="284">
<div>Edit Existing Images</div>
<div>
<form id="form2" name="form2" action="/ifolio/userimages.php"
method="post">
<p align="center"><font color="#CCCCCC" size="1" face=
"verdana"><strong>Select Image<br></strong></font></p>
<p align="center"></p>
<table border="0">
<tr>
<td><select name="choiceimage" size="7" onchange="pre()">
<option value="1">Image1</option>
 
<option value="2">Image2</option>
<option value="3">Image3</option>
<option value="4">Image4</option>
</select></td>
<td><img src="gallery/noth.gif" border="0" name="preview" alt=
"#########"></td>
</tr>
</table>
<p align="center"><font color="#CCCCCC" size="1" face=
"verdana"><strong><strong><font color="#CCCCCC" size="1" face=
"verdana"><input type="submit" name="submit" value="Edit">
<input type="hidden" name="password" value=""> <input type="submit"
name="submit" value="Delete"></font></strong></strong></font></p>
</form>
</div>
</div>
<!-- MAIN CONTENT BOX END --></td>
 
<td width="15" height="773" rowspan="3"><img src=
"images/spacer.gif" width="15" height="773" alt=""></td>
</tr>
<tr><!-- FOOTER START -->
<td background="images/MIDDLE_05.gif" width="787" height="141">
<div id="TOP-FootHead1" width="230" height="111">
<div>Annual User Award</div>
<p>ZypheR2006 won top user for 2007 and was awarded 5000
credits!!!</p>
</div>
<div class="style1" id="TOP-FootHead2" background=
"images/TOP_FootHead2.gif" width="230" height="111">
<div>Monthly User Award</div>
<p>MaryPoppins was Decembers winner for the free access to all
tools</p>
</div>
<div id="TOP-FootHead3" background="images/TOP_FootHead3.gif"
width="230" height="111">
 
<div>Weekly User Award</div>
<p>DrDre2001's ifolio use for last week wins him 100 credits.</p>
</div>
<!-- Footer with User Award Details --></td>
</tr>
<!-- FOOTER END -->
<tr>
<td width="787" height="12"><img src="images/spacer.gif" width=
"787" height="12" alt=""></td>
</tr>
</table>
</div>
</body>
</html>

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 2

Author Comment

by:Ryan Bayne
ID: 20828215
looks a mess!!!!!
0
 
LVL 19

Accepted Solution

by:
v2Media earned 2000 total points
ID: 20832125
Try overwriting your js functions with those below, and give the preview image an id tag:

<img src="gallery/noth.gif" border="0" name="preview" id="preview" alt="#########">
<script language="JavaScript" type="text/javascript">
function change(html){
	document.getElementById('descriptions').innerHTML=html;
}
function pre() {
	document.form2.preview.src = 'gallery/thumbnails/image' + document.form2.choiceimage.options[document.form2.choiceimage.selectedIndex].value+'.jpg';
}
</script>

Open in new window

0
 
LVL 19

Expert Comment

by:v2Media
ID: 20832133
correction:
<script language="JavaScript" type="text/javascript">
function change(html){
	document.getElementById('descriptions').innerHTML=html;
}
function pre() {
	document.getElementById('preview').src = 'gallery/thumbnails/image' + document.form2.choiceimage.options[document.form2.choiceimage.selectedIndex].value+'.jpg';
}
</script>

Open in new window

0
 
LVL 2

Author Comment

by:Ryan Bayne
ID: 20871564
That works a treat. Next I need to figure out why changing the scripts environment made it not work thanks for that

:-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question