Link to home
Start Free TrialLog in
Avatar of CementTruck
CementTruck

asked on

Need help with auto form submit or similar

Hello experts,

I'm in a little bit of a sticky wicket. I have to create pages that submit form info to pages that already exist and are convoluted enough that I should not tamper with their existing code flow.

Here's the rub:

Existing (original) page - I have added a form in a cfloop. The form replicates itself until the cfloop ends. On click, the form submits to a processing page that updates a database table using a recid that was submitted that was generated by the loop.

Processing page - The processing page is ultra simple. It is just an update statement.

I need to get back to the original page, but I need to pass form values back to it so it displays the original query with the updated values.

I want to do one of two things. I want to either do some type of "history -1" deal at the end of the processing page, but it needs to be automatic without the user having to click anything, or an automatic form submit with a bunch of hidden form values that would be pre-populated on page load from the previous (original) page.

I like the "history -1" option, but can't seem to figure out how to do this automatically. I can do it via button-click, but don't want a the user to have to do anything, and using cflocation would actually reload the original page and all form values that were used for query data would be lost.

Or, is there a way to stay on the original page and just have processing happen and have the original page just reload with the current form data that was passed to it to begin with?  This can be done with tons of AJAX, but like I said the page is so convoluted I don't even want to try and introduce something that would take me a week to get working. The processing page is working just fine; I just need the last piece of the puzzle.

I hope I explained my predicament well enough.
Avatar of _agx_
_agx_
Flag of United States of America image

I need to get back to the original page, but I need to pass form values back to it so it displays the original query with the updated values.

Assuming the form uses method=POST, a simple option is to create a hidden form on the action page. Just loop through the FORM structure to populate all the fields

   <form id="transferForm" method="post" action="yourOriginalPage.cfm">
       <cfloop collection="#FORM#" item="key">
       <cfoutput>
           <input type="hidden" name="#key#" value="#HTMLEditFormat(FORM[key])#">
       </cfoutput>
       </cfloop>
   </form>

... and do an automatic javascript form.submit() on page load:

          document.getElementById('transferForm').submit();

Note, like with the history() option - javascript must be enabled for this to work.
Not sure I quite understand what you are doing but another option might be to use cfhttp to post to your pages.

Rather than loop the form, pass the form data to a page which loops cfhttp POSTs then you can control the flow....the processing can then reload the original page when done.

or not...
Avatar of CementTruck
CementTruck

ASKER

@_agx_

Here is the extent of my processing page. The update query works, but the form does not submit itself. What am I doing wrong?



<!--- Confirm authentication / Session timeout--->
<CFINCLUDE template="incAuth.cfm">
<!--- <CFINCLUDE template="incTrackingStats.cfm">--->

<cfquery  datasource="MyDB">
    UPDATE invItems
    SET auditWho= '<cfoutput>#Session.username#'</cfoutput>, auditDate = <cfoutput>#now()#</cfoutput>
    WHERE RecID = <cfoutput>#URL.id#</cfoutput>
</cfquery>

<html>
<head>

<script type="text/javascript">
function submitform()
{
  document.getElementById('transferForm').submit();
}
</script>


<title>Inventory Audit Update</title>
</head>

<body>

   <form id="transferForm" method="post" action="invMainResultsNew.cfm" >
           <input type="hidden" name="SEARCHHERE" value="<cfoutput>#form.searchhere#</cfoutput>">
   </form>

</body>

</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@SidFishes,

I have a page that lists all assets our company owns. The table is generated by a query/loop. You can view all assets, or all assets by building. Each of these tables are column sortable. There is a lot of page refreshing going on, and lots of opportunity to really screw something up in an old page that already works, and so I was just trying to limit my impact on the page by containing my contribution to one small fraction of the page.  

I inserted a <td></td> tag at the end of the table, and within that set of TD tags I inserted a form with a button that simply states "audit" and a hidden field with the loop's corresponding recordID. Because the TD tags are in a loop, that form is duplicated until the loop runs out. Clicking on the "audit" button sends the hidden field's recid to the processing page and the update statement adds or changes the AuditDate column and UserID column to today's date and the UserID of the person that is signed on. It is a really rudimentary change. All I wanted to do was get the processing page to resubmit a form automatically back to the page it came from so that the original page could then refresh with all the values it needs to get the user back to exactly the same view he/she had before the last submit. The table's location info and sort are dependent upon the if statements that clutter the "where" statement on the query, so I wanted to pass that along in the auto submit so that the user feels no pain. It is old school and requires a lot of page reloads, but it would be easier than recreating this massive page. I've only give enough info that is pertinent to the problem at hand as the page does so many more functions that have nothing to do with this issue, but is affected by the things passed along in the form submission.

I hope that clears it up.
That was totally it!!! <body onload="submitform()">.

I always miss the little things when the blinders are on and I've been working on something for far too long.

Thanks on the tip about the injection. I did implement the change you suggested although it is an intranet site and so I'm not as worried about that happening.

Thanks for the usual spot on help you provide.
Maybe you could do a form POST to a hidden iframe

<cfoutput><html>
<body>
	
<cfset TempQry= querynew("")>
 
<cfset queryaddcolumn  (TempQry, "Col1", "CF_SQL_integer", ListToArray("1,2,3,4,5,6"))>
<cfset queryaddcolumn  (TempQry, "Col2", "cf_sql_varchar", ListToArray("ab321, cf231, ghsd32, sdh43, jjksl3, kddkl33"))> 
 	
<cfloop query="tempqry">
<form name="form1" action="iframeform.cfm"
        method="post" target="hif" 
            onsubmit="document.getElementById('hif').src='iframeform.cfm';">    

    <input type="hidden" name="recid" value="#tempqry.col2#">
    <input type="submit" value="Submit" name="B1">

</form>
</cfloop>	
<iframe name="hif" id="hif" width="500" height="500"></iframe>
</body>
</html> </cfoutput>

Open in new window


----iframeform.cfm----

<cfoutput>
Do the Processing the record for #form.recid#
</cfoutput>

Open in new window



I've set the size of the iframe so you can see it but you can just change values to 0
or what agx said ;P
Yeah, normally I'd suggest going with something slicker like ajax or an iframe like sid suggested. But given that you want to minimize the impact, the old hidden form.submit() is probably the way to go :)
@SidFishes,

Thanks for responding. I normally search through my old questions from time to time so I'm sure I'll use your idea eventually. Your contributions are thoroughly appreciated.

@_agx_,

Although multi port fuel injected, double overhead cam, paddle shifter havin', all wheel drive, 240mph capable vehicles are always cool, sometimes an old carbureted, 4-on-the-floor, that has no AC and a missing passenger seat, with a top speed of 45mph is the right tool for the job.   ;)

Thank you both.
Hey, it doesn't *have* to go 240mph, 230 would be doable ;-)  

Seriously I work on a mix of apps, so I know what you mean. Sometimes you really need the new features, sometimes not.