Form in PHP/Javascript Tabbed Pane

Hi to all,

I have a form (with a input field and submit) in Facilities Tab of my Tabbed Pane. I php searches for the number of existing orders and then shows up those many number of forms.When I click submit, it has to check for, whether the attachement is browsed and selected. In case there is no attachment, it must focus back to the Browse field. with an alert statement
But, when we click Submit button, it says

document.all.t2Contents.forms is null or not object..

Am I wrong some where?

I have been trying with document.getElementsById and getElementsBy Name. But the problem still exist, since I generate multiple forms based on the database, the form name and element name keep on changing, its creating a problem..
If any body knows the solution, please let me know..

(Code below). Appreciate your help. Thanks in advance for your time and expertise.

Chidu
chidanandmba@gmail.com

<?php
ob_start();
session_start();
require_once ("checksession.php");
require_once ("../config.php");
require_once ("../classes/database.php");
$db = new database();

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>MGH ::: <?php echo $type;?></title>
<link href="logo.css" rel="stylesheet" type="text/css">
<script language="JavaScript" type="text/JavaScript">
//a public function that the container uses to pass in values for the labels
function public_Labels(label1, label2){
t1.innerText = label1;
t2.innerText = label2;
}
//a public function that the container uses to pass in values for the card containers
function public_Contents(contents1, contents2){
t1Contents.innerHTML = contents1;
t2Contents.innerHTML = contents2;
init();
}
//sets the default display to tab 1
function init(){
tabContents.innerHTML = t1Contents.innerHTML;
}
//this is the tab switching function
var currentTab;
var tabBase;
var firstFlag = true;
function changeTabs(){
if(firstFlag == true){
currentTab = t1;
tabBase = t1base;
firstFlag = false;
}
if(window.event.srcElement.className == "tab"){
currentTab.className = "tab";
tabBase.style.backgroundColor = "white";
currentTab = window.event.srcElement;
tabBaseID = currentTab.id + "base";
tabContentID = currentTab.id + "Contents";
tabBase = document.all(tabBaseID);
tabContent = document.all(tabContentID);
currentTab.className = "selTab";
tabBase.style.backgroundColor = "";
tabContents.innerHTML = tabContent.innerHTML;
}
}

function getAttachment(form_name)
{
if(document.all.t2Contents.forms[form_name].elements["attachment"].value=='')
{
alert("Enter Attachment");
document.all.t2Contents.forms[form_name].elements["attachment"].focus();
return false;
}
return true;
}

</script>
</head>
<BODY onclick="changeTabs()" onload="init()">
<div align="center">
<br>
<div align="center">
<TABLE BGCOLOR=#C0C0C0 width="800" CELLPADDING=0 CELLSPACING=0>
<TR>
<TD ID=t1 CLASS=selTab HEIGHT=25 WIDTH="15%">Items</TD>
<TD ID=t2 CLASS=tab HEIGHT=25 WIDTH="15%">Facilities</TD>
<TD ID=t3 CLASS=noTab STYLE="background-color:white"></TD>
</TR>
<TR>
<TD ID=t1base STYLE="height:2; border-left:solid thin white"></TD>
<TD ID=t2base STYLE="height:2; background-color:white"></TD>
<TD ID=t3base STYLE="height:2; background-color:white"></TD>
</TR>
<TR>
<TD HEIGHT="*" COLSPAN=3 ID=tabContents
STYLE="border-left:solid thin gray;
border-bottom:solid thin gray;
border-right:solid thin gray">sample contents</TD>
</TR>
</TABLE>
</div>
<br>
<!-- ITEMS TAB -->
<DIV CLASS=conts ID=t1Contents>
<table width="800" border="1" cellpadding="1" cellspacing="0" bordercolor="#CCCCCC">
<tr align="center" valign="middle">
<td width="30%" class="boldtext">Item</td>
<td width="10%" class="boldtext">Code</td>
</tr>

</table>
</DIV>
<!-- FACILITIES TAB -->
<DIV CLASS=conts ID=t2Contents>
<table width="800" border="1" cellpadding="1" cellspacing="0" bordercolor="#cccccc">
<?php
$facilitydisplay = $order->getDisplayfacility($orderid); //search for all the records w.r.t order id
$faccount=-1;
foreach($facilitydisplay as $facidisply)
{
$faccount++;
$faciattachments= $order->getFacility1($facidisply['facility_code'],$orderid); // search for all attachements for the order id
echo "<tr valign=\"top\" class=\"e".($faccount & 1)."\">";
foreach($faciattachments as $attachments)
{
$filename = $attachments['file_name'];
$attachment = $attachments['attachment'];
?>
<td width="55%" align="left" class="text"> Attachments: </td>
<td valign="top" align="right">
<?php $attachment_form_name = 'attachment'.$faccount; ?>
<form name="<?php echo $attachment_form_name; ?>" method="post" action="" enctype="multipart/form-data" onSubmit="return getAttachment('<?php echo $attachment_form_name; ?>');">
<input name="attachment" type="file" class="text" size="35">
<input name="Submit" type="submit" class="buttonstyle" value="Attach">
</form>
</td>
</tr>
<?php
}
}
?>
</table>
</DIV>
</div>
</body>
</html>

With Regards
Chidanand. G.B
[6382 byte] By [chidanandgb_2000] at [2007-11-20 7:16:04]
# 1 Re: Form in PHP/Javascript Tabbed Pane
I know you say that you have tried getElementByName(), but I would have to assume you did not implement it properly. I might be wrong. Try...

function getAttachment(form_name){
var theForm = document.getElementById(form_name);
var theAttachment = document.getElementById('attachment');
if(theForm.theAttachment.value == ''){
alert("Enter Attachment");
theForm.theAttachment.focus();
return false;
}
return true;
}

<form ... id="<?php echo $attachment_form_name; ?>" ... onSubmit="return getAttachment('<?php echo $attachment_form_name; ?>');">

<input name="attachment" id="attachment" type="file" class="text" size="35">
PeejAvery at 2007-11-8 0:42:22 >
# 2 Re: Form in PHP/Javascript Tabbed Pane
Thanks a lot Dude...

I shall try the best, and hope atleast this thing worx....
chidanandgb_2000 at 2007-11-8 0:43:14 >
# 3 Re: Form in PHP/Javascript Tabbed Pane
Hi PeejAvery,

I am still getting the same message...

Anybody here? who can help me? Please..!

I need a solution for the issue...

With Regards,
Chidanand. G.B
chidanandgb_2000 at 2007-11-8 0:44:21 >
# 4 Re: Form in PHP/Javascript Tabbed Pane
Okay, then you are still setting something up wrong in the calling or the timing of the function call. Since you have PHP source in the code, we can't run a test script ourselves. So, why don't you view the source of the page. Copy and paste that here so that we can work with the HTML/JavaScript only part.
PeejAvery at 2007-11-8 0:45:18 >
# 5 Re: Form in PHP/Javascript Tabbed Pane
Hi there, I have just found a solution for the problem, I just thought of sharing it with you all

<form ... id="<?php echo $attachment_form_name; ?>" ... onSubmit="return getAttachment(this);">

<input name="attachment" id="attachment" type="file" class="text" size="35">

instead of return getAttachment('<?php echo $attachment_form_name; ?>');

use onSubmit="return getAttachment(this);" and that solves the problem..

Thanks..
chidanandgb_2000 at 2007-11-8 0:46:22 >
# 6 Re: Form in PHP/Javascript Tabbed Pane
Hi to everyone,,,

I have just one more small request on : in the previous code, when things started to work, I have a small difficulty, I am using the following code:
<td valign="top" align="right">
<?php $attachment_form_name = 'attachment' . $faccount; ?>
<iframe name="theFrame" width=0 height=0 frameborder=0><script language="Javascript" >window.location.reload(true);
</script></iframe>
<form name="<?php echo $attachment_form_name; ?>" id="<?php echo $attachment_form_name; ?>" method="post" action="" enctype="multipart/form-data" onSubmit="return getAttachment(this);" target="theFrame">
<input name="attachment" type="file" class="text" size="35">
<input name="Submit" type="submit" class="buttonstyle" value="Attach">
</form>

The code seems to upload a file to the server, but when i click submit button, it uploads the file, very well. no problem at all, But what I need is to refresh the same tab and load it back. Using the iframe as suggested by PeejAvery, I can see that, it stays in Tab 2, But the part of the page needs to get refreshed, How can I do that? any code in php or javascript...

Help Appreciated, Thanks in advance for your time and expertise..

With Regards,
Chidanand. G. B
chidanandgb_2000 at 2007-11-8 0:47:27 >
# 7 Re: Form in PHP/Javascript Tabbed Pane
You can only refresh a whole page by using reload(). If you want to only "refresh" part of a page, you will have to dynamically re-write the content using innerHTML = "...".
PeejAvery at 2007-11-8 0:48:22 >
# 8 Re: Form in PHP/Javascript Tabbed Pane
Thanks for the advice, PeejAvery

But, when a form is submitted and no action other than the javascript validation, you said it will not reload the whole page, and when I wanted the page to stick to tab 2 , you asked me use iframe and target the form to the frame. When we do so, then why isn't the page contents get refreshed, well I need an empty <input type="file"> after a successfull file upload.. Atleast, how can that be done?

Can we call another function or something like that?

With Regards,
Chidanand
chidanandgb_2000 at 2007-11-8 0:49:25 >
# 9 Re: Form in PHP/Javascript Tabbed Pane
If you want to reload the page...
window.location.reload();
If you want to just reset the form...
document.FORMNAME.reset();
PeejAvery at 2007-11-8 0:50:31 >