Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing checkboxes dynamically

Status
Not open for further replies.

cajchris

Programmer
Oct 13, 2005
57
GB
i am implementing a news forum whereby users can upload attachments for a message. While at the moment i can successfully iterate through the list of attachments in the page context and display them as checkboxes, i need some way of being able to remove attachments. so when the remove button is clicked i can remove that check box as well as its entry in the page context list. below is the code for my JSP.

<%@ page language="java" import="javazoom.upload.*,java.util.*, java.io.*, com.mediasurface.client.IItem, com.mediasurface.datatypes.ItemKey, com.mediasurface.client.ISite, com.mediasurface.client.IType" %>
<%@ page errorPage="ExceptionHandler.jsp" %>
<%@ include file="/WEB-INF/components/general/tagDirectives.inc"%>


<jsp:useBean id="upBean" scope="page" class="javazoom.upload.UploadBean" >
<jsp:setProperty name="upBean" property="filesizelimit" value="<%= 1024 %>" />

<req:request id="theRequest" />
<script language="Javascript" type="text/javascript">
<!--
function checkForm()
{
var mform = document.addMessageForm;
var nonvalid='';
if ( mform.summary.value.match( /\S/ ) == null ) { nonvalid += "- Please enter a message summary\n"; }
if ( mform.bodytext.value.match( /\S/ ) == null ) { nonvalid += "- Please enter your message details\n"; }

if ( nonvalid == '' ) { return true; }
else { alert( nonvalid ); return false; }
}
//-->
</script>


<div class="holder">
<form action="?<jsp:getProperty name="theRequest" property="queryString" />&amp;submit" method="post" onsubmit="return checkForm();">
<input type="hidden" name="summary" value="Topic Reply" />
<fieldset>
<table cellpadding="0" cellspacing="0" border="0" class="formTable" summary="Add a reply">
<colgroup>
<col width="20%" />
<col width="80%" />
</colgroup>
<tr>
<td class="formLabel" valign="top"><label for="addtopic">Add a reply</label></td>
<td><textarea name="bodytext" id="addtopic" class="formTextarea" cols="25" rows="4"></textarea></td>
</tr>
</table>

</fieldset>

<div class="submitButton">
<input type="submit" value="ADD" class="button" />
</div>
</form>

<%

String filepath = "";
Integer topicItemId;
int forumItemId = 0;
try
{
topicItemId = Integer.valueOf(pageContext.getRequest().getParameter("topic"));
}
catch (Exception e)
{
topicItemId = null;
}

if (topicItemId != null)
{
IItem topicItem = null;
try
{
topicItem = ms.getItem(ctx, new ItemKey(topicItemId.intValue()));
}
catch (Exception e)
{
//out.println("Failed to find topic with id " + topicItemId, e);
}

if (topicItem != null)
{
try
{
forumItemId = topicItem.getParent().getKey().getIntKey();

}
catch (Exception e)
{
//out.println("Failed to find forum containing topic with id " + topicItemId, e);
}

}
}
filepath = "C:"+ File.separator + "forum_" +forumItemId + File.separator + "topic_" +topicItemId;
pageContext.setAttribute("filepath",filepath);
%>

<jsp:setProperty name="upBean" property="folderstore" value="<%=filepath%>" />
</jsp:useBean>
<br></br>
<br></br>
<br></br>
<p>
<%

String attachmentName = null;
String testName = "";
ArrayList d_attachments = null;

if (MultipartFormDataRequest.isMultipartFormData(request))
{

// Uses MultipartFormDataRequest to parse the HTTP request.
MultipartFormDataRequest mrequest = new MultipartFormDataRequest(request);
String todo = null;

if (mrequest != null) todo = mrequest.getParameter("todo");

if ( (todo != null) && (todo.equalsIgnoreCase("upload")) )
{
Hashtable files = mrequest.getFiles();
if ( (files != null) && (!files.isEmpty()) )
{
UploadFile file = (UploadFile) files.get("uploadfile");
File uploadDirectory = new File(upBean.getFolderstore());
String[] uploadedFiles = uploadDirectory.list();

d_attachments = (ArrayList)pageContext.getAttribute("attachmentList", PageContext.SESSION_SCOPE);
System.out.println("d_attachments is " + d_attachments);

if(d_attachments != null)
{

//if(d_attachments.size() < 4)
// {

if (file != null)
{

if(file.getFileSize() <= upBean.getFilesizelimit())
{
// Uses the bean now to store specified by jsp:setProperty at the top.
upBean.store(mrequest, "uploadfile");
pageContext.setAttribute("originalfilename",file.getFileName(), PageContext.REQUEST_SCOPE);
File uploadedFile = new File(upBean.getFolderstore() + File.separator + file.getFileName());
File timestampedUploadedFile = new File(upBean.getFolderstore() + File.separator + System.currentTimeMillis() + "_"+file.getFileName());

boolean success = uploadedFile.renameTo(timestampedUploadedFile);
if (success)
{
pageContext.setAttribute("attachmentname",timestampedUploadedFile.getAbsolutePath(), PageContext.REQUEST_SCOPE);
}
}
else
{
out.println("<li>Uploaded File Size = "+file.getFileSize()+"!</li>");
out.println("<li>The attachment size must not exceed "+upBean.getFilesizelimit()+"!</li>");
}
}
//}

//else
//{
// out.println("<li>Only 4 files can be uploaded per message</li>");
//}

}


else{

if (file != null)
{

if(file.getFileSize() <= upBean.getFilesizelimit())
{
// Uses the bean now to store specified by jsp:setProperty at the top.
upBean.store(mrequest, "uploadfile");
pageContext.setAttribute("originalfilename",file.getFileName(), PageContext.REQUEST_SCOPE);
File uploadedFile = new File(upBean.getFolderstore() + File.separator + file.getFileName());
File timestampedUploadedFile = new File(upBean.getFolderstore() + File.separator + System.currentTimeMillis() + "_"+file.getFileName());

boolean success = uploadedFile.renameTo(timestampedUploadedFile);
if (success)
{
//out.println(timestampedUploadedFile.getAbsoluteFile());
pageContext.setAttribute("attachmentname",timestampedUploadedFile.getAbsolutePath(), PageContext.REQUEST_SCOPE );
}
}
else
{
out.println("<li>Uploaded File Size = "+file.getFileSize()+"!</li>");
out.println("<li>The attachment size must not exceed "+upBean.getFilesizelimit()+"!</li>");
}
}
}
}
else
{
out.println("<li>No uploaded files</li>");
}
}
else out.println("<BR> todo="+todo);
}

%>
<c:eek:ut value="${originalfilename}"/>

<form method="post" action="#" name="upform" enctype="multipart/form-data">
<table cellpadding="0" cellspacing="0" border="0" class="formTable">
<tr>
<td align="left"><b>Select a file to attach to your message (maximum of 4 files) :</b></td>
</tr>
<tr>
<td align="left">
<input type="file" name="uploadfile" size="50">
</td>
</tr>
<tr>
<td align="left">
<input type="hidden" name="todo" value="upload">
<input type="submit" name="Submit" value="Upload">
<input type="reset" name="Reset" value="Cancel">
</td>
</tr>
</table>

</form>
</p>
</div>

<c:if test="${attachmentname != null}">
<ho:forumAttachmentHelper var="attachmentname" />
</c:if>

<c:if test="${originalAttachmentNameList != null}">
<form method="post" action="#" name="upform" enctype="multipart/form-data">
<c:forEach items="${originalAttachmentNameList}" var="token">
<input type="checkbox" name="attachment"><c:eek:ut value="${token}"/>
</c:forEach>
</c:if>

the main section of interest is the above form which shows where the check boxes are made. The 'originalAttachmentList' is set in the page context from an associated java file whenever a new attachment is added.

The inline java in the code deals with firstly setting up the filepath for the attachment, and secondly taking an uploaded file and storing it on disk, and adding it to a arrayList in the page contexts

Obviously i will need some way in this inline java to determine which checkboxes were removed so that i can remove them from the page context array list

regards,
cajchris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top