SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Changing the action of a form using javascript

    Hello all,
    I have the following piece of code. What I am trying to do here is to change the action of a form to another page when a submit button is clicked.

    The following code works fine in IE but not on FF.
    Code:
    <script language="javascript">
    function preview(id)
    {
        this.frmNewTopic.method='post';
    	this.frmNewTopic.action='preview.cfm?f='+id;
    	  
    }
    
    </script>
    <cfparam name="url.f" default="1">
    <cfparam name="errSub" default="">
    <cfparam name="errmessage" default="">
    <cfparam name="form.subject" default="">
    <form name="frmNewTopic"  method="post" action="<cfoutput>#cgi.SCRIPT_NAME#?f=#url.f#</cfoutput>">
    <input type="hidden" name="forumID" value="<cfoutput>#url.f#</cfoutput>">
    
    <table width="100%" border="0" >
    			
    			<tr>
    			  <td colspan="2" align="left"><font class="rederror"><cfoutput>#errSub#</cfoutput></font></td>
    			  </tr>
    			<tr> 
    			<td width="22%" align="left"><b>Subject: </b> </td>
    			<td width="78%" align="left"><input name="subject" type="text" class="formFieldInput" value="<cfoutput>#form.subject#</cfoutput>"></td>
    			</tr>
    			<tr>
    			  <td colspan="2" align="left"><strong>Message</strong><font class="rederror">&nbsp;&nbsp;&nbsp;<cfoutput>#errmessage#</cfoutput></font></td>
    			  </tr>
    			<tr>
    			  <td colspan="2" align="left">
    					<textarea name="description" cols="90" rows="10"></textarea>	  
    				</td>
    			  </tr>
    			<tr>
    			  <td  align="left">
    			    <input type='button' name="btnCancel" value="Cancel" onclick="javascript:document.location ='forum.cfm?f=<cfoutput>#url.f#</cfoutput>';" />
    				
    				<input type='submit' name="btnSubmit" value="Post"  />
    				<input type='submit' name="btnPreview" value="Preview Post" onClick="javascript:preview(<cfoutput>#url.f#</cfoutput>)" />
    				
    				
    			  </td>
    			  
    			  <td  align="right">&nbsp;
    			  </td>
        </tr>
    	   </table>
    </form>
    Am I missing out anything here?Your help on this problem is much appreciated.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think your use of "this" is wrong for the context, try:
    Code:
    function preview(id)
    {
    	document.forms["frmNewTopic"].method='post';
    	document.forms["frmNewTopic"].action='preview.cfm?f='+id;
    	
    	// use something like this next line to verify it's working
    	// alert(document.forms["frmNewTopic"].action);
    }
    Also, the onclick event of your submit button does not need javascript: in front of the function call.

  3. #3
    SitePoint Enthusiast birnam's Avatar
    Join Date
    Oct 2006
    Location
    Kansas City
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might also try passing "this.form" in as a second variable through your onClick:

    Code:
    onClick="javascript:preview(<cfoutput>#url.f#</cfoutput>, this.form)"
    and then

    Code:
    function preview(id, frm)
    {
    	frm.method='post';
    	frm.action='preview.cfm?f='+id;
    }
    If that still doesn't work, you can try:

    Code:
    function preview(id, frm)
    {
    	frm.method='post';
    	frm.setAttribute('action', 'preview.cfm?f='+id);
    }
    quality web development at Birnam Designs
    articles and tips for developers at Hunk of, Hunk of Birnam Love


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •