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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set the html:form action dynamically?

Status
Not open for further replies.

SuperMoonster

Programmer
Aug 11, 2005
59
BR
Hello everyone, I have a form with 2 html:submit buttons. I want to change the action of the form dynamically: if I click button 1, it call action 1. If I click button 2, it calls action 2. I tried using javascript, but didn't work.I did the following:

Code:
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@taglib uri="[URL unfurl="true"]http://java.sun.com/jsp/jstl/core"[/URL] prefix="c"%>
<%@ page import="auge.conexao.ColecaoService" %>
<%@ page import="java.util.*" %>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]

<html:html locale="true">
<head>
   <title>Coleção</title>
   <html:base/>
   
</head>
<body bgcolor="white">

   <script language="JavaScript">
      function go(param) {
        if (param == 1) {
          document.forms[0].action.value = "/UpdateColecao.do";
        }
        else {
          document.forms[0].action.value = "/DelColecao.do";       
        }
          document.forms[0].submit();
        }
   </script>
   
    <span style="font-family:Sans-serif;">
   
    <table width="100%" align="center">
    <tr><td> Coleção </td></tr>
    <tr bgcolor="#000000"> <td></td></tr>
    </table>

   <br><br><br><br>
   <html:errors/>
   <br>
   
   <html:form action="/DelColecao.do" method="post" focus="colecao"> 
     <table border="0" align="center">
         <tr><td width="50%"> Coleção </td><td width="50%"><html:text property="colecao" value="${requestScope.col.colecao}" readonly="true"/></td></tr>     
         <tr><td width="50%"> Descricão </td><td width="50%"><html:text property="descricao" value="${requestScope.col.descricao}"/></td></tr>
         <tr>
            <td width="50%" align="center">
              <html:button property="Gravar" value="Gravar" onclick="javascript:go(1)"/>
            </td>
            <td width="50%" align="center">
              <html:button property="Excluir" value="Excluir" onclick="javascript:go(2)"/>
            </td>
         </tr>     
      </table><br/>
   </html:form>

   </span>
</html:html>

as you see, I have a javascript and I'm trying to call it at the onclick event on the button. Isn't working. Does anyone know why?

Does anyone have a better idea? Maybe there's a struts-way to do it...

Sorry if I keep asking a lot of stupid questions. I'll try to make it the last one.

Thanks very much
 
Hi

It should be document.forms[0].action = "/UpdateColecao.do";

Cheers
Venu
 
Thank you very much! It worked: now, it does change the action when I click. The problem is that when it calls the action, it gives me a error saying: The requested resource (/UpdateColecao.do) is not available."


It's not a struts problem, cause it works when I take out the javascript and call the action directly from the form. Would you know what that is? My code is as follows:

Code:
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@taglib uri="[URL unfurl="true"]http://java.sun.com/jsp/jstl/core"[/URL] prefix="c"%> 
<%@ page import="auge.conexao.ColecaoService" %>
<%@ page import="java.util.*" %>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]

<html:html locale="true">
<head>
   <title>Coleção</title>
   <html:base/>
   
</head>
<body bgcolor="white">

   <script language="JavaScript">
      function go(param) {
        if (param == 1) {
          document.forms[0].action = "/UpdateColecao.do";
        }
        else {
          document.forms[0].action = "/DelColecao.do";        
        }
        }
   </script>
   
    <span style="font-family:Sans-serif;">
    
    <table width="100%" align="center">
    <tr><td> Coleção </td></tr>
    <tr bgcolor="#000000"> <td></td></tr>
    </table>

   <br><br><br><br>
   <html:errors/>
   <br>
   
   <html:form action="/UpdateColecao.do" method="post" focus="colecao">  
     <table border="0" align="center">
         <tr><td width="50%"> Coleção </td><td width="50%"><html:text property="colecao" value="${requestScope.col.colecao}" readonly="true"/></td></tr>      
         <tr><td width="50%"> Descricão </td><td width="50%"><html:text property="descricao" value="${requestScope.col.descricao}"/></td></tr>
         <tr>
            <td width="50%" align="center">
              <html:submit value="Gravar" onclick="javascript:go(1)"/>
            </td>
            <td width="50%" align="center">
              <html:submit value="Excluir" onclick="javascript:go(2)"/>
            </td>
         </tr>      
      </table><br/>
   </html:form> 

   </span> 
</html:html>
 
Geez, what was I thinnking with all the javascript?

If anyone has this problem, I suggest to solve it with LookupDispatchAction.

See ya.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top