Saturday, March 21, 2009

Submit Form through Windows Modal Dialog

Yes, you can submit form on Windows modal dialog.

Summary

Set the name of the modal dialog to let say ‘myModal’

The form inside the window modal dialog should have the target set to _self or name ‘MyModal’ and submit the form.

Details with Code

We will have three jsps,

1) The window modal opener (modalTest.jsp)

2) Window Modal Form Page (modelD.jsp)

3) Modal Form Submit page (modelSubmit.jsp)

modalTest.jsp

<html>
<body>
<head>
<script language="javascript">
function openModal(url, width, height)
{
var features = "";
if (width != "")
{
features = features + "dialogWidth:" + width + "px;";
}
if (height != "")
{
//features = features + "dialogHeight:" + height + "px;help:no;edge:sunken;status:no;unadorned:yes";
features = features + "dialogHeight:" + height + "px;";
}

return window.showModalDialog(url, "myModal", features);

}

function showWindow()
{
var modalWin;
modalWin = openModal('modelD.jsp', 545, 500);
}

</script>
</head>
<a href="#" onclick="showWindow();" > Click </a>
</body>
</html>

modelD.jsp

<html>
<head>
<title>My Modal Window</title>
<script language="javascript">

window.name = 'myModal';
</script>
</head>
<body>
<form name="frm" action="modalSubmit.jsp" target='myModal'>

<input type ="text" name="test1">
<input type="button" value="Submit Modal Window" onclick="document.frm.submit();" >

</form>
</body>
</html>

modelSubmit.jsp

<html>
<head>
<title>Modal Dialogue Submitted</title>
</head>
<body>
<%
String param = request.getParameter("test1");
if(param == null)
param="empty text box sent";
%>
<h1>
<%=param.toUpperCase() %>
</h1>
<br>
Hence you can submit a modal dialoague........... I want a pizza...
<br>
<input type="button" value="Close me" onclick="window.close();" />
</body>
</html>

Save all these jsp to you JSP Containers (Tomcat, JBOSS or any)

call modalTest.jsp

The first screen you gonna see is aa simple click screen that you will click to open the windows modal dialog

m1

On windows modal dialog you will see a form with a text field,

m2

write your name in the text field in lower caps and submit the form… and yes the form submits and return the result with your name in Capital letters

m3

That it, you can submit Windows Modal dialog.

3 comments:

Anonymous said...

It works Thanks buddy....

Anonymous said...

Thanks a lot... you saved my day....

Anonymous said...

Thanks a lot. It worked for me. I used
base target="_self"