Question

How to use Javascript to submit page in background that queries database and pulls info to fill in the remaing form?

Asked by: trainmom

I would like to be able to do this, without refreshing page and without submitting to a new form.

After user fills in color type via radio button, have a JS to query a database and find all matches for the color entered and
return those matches so that remaining form fields are filled in.

I only need this for IE browser (not any of the others).

Is there a quick way to do this?

Thanks In Advance.
Trainmom

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-03-29 at 09:09:49ID20936125
Tags

javascript

,

submit

,

page

Topic

ColdFusion Application Server

Participating Experts
4
Points
500
Comments
11

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. radio and submit
    OK, two radio buttons, none selected. One submit button. When user selects a radio, then clicks submit, they will be directed to a particular url. If no radio is selected, then they will go to a default URL. Anyone know how to do this? Cannot use cgi. Thanks
  2. on submit refresh
    I don't know how to do code which will refresh the page when user click submit button. Anybody can help?
  3. PHP/javascript self submit and refresh
    How to refresh a self submitted form in order to up to date array of records in the same form ? My form displays a list of records provided by PHP/MySQL select query. Through this form, user can then enter an ID of record he wants to delete and then submit form to itself ...
  4. Submitting a form from javascript
    Hi, I am struggeling with a problem I know has a solution - somehow..... I have a thispage.jsp wich is to be refreshed every x seconds, using the <meta refresh> tag. I want this page to scroll to where the user had scrolled to upon the refresh, and not to the top ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: mrichmonPosted on 2004-03-29 at 09:49:20ID: 10706062

Okay this is done in a few steps.

Step 1) include the following file as a javascript file and save as iFrameRPC.js

// ****************BEGIN FILE******************************************
var IFrameObj; // our IFrame object

function callToServer(backgroundURL) {
      if (!document.createElement)
      {// todo :: implement some kind of redirection if browser doesn't support creating of dom object
            return true;
      }
        
      var IFrameDoc;
      var URL =  backgroundURL;
      if (!IFrameObj && document.createElement)
      {// create the IFrame and assign a reference to the object to our global variable IFrameObj.
            // this will only happen the first time callToServer() is called
            try
            {
                  var tempIFrame=document.createElement('iframe');
                  tempIFrame.setAttribute('id','RSIFrame');
                  tempIFrame.style.border='0px';
                  tempIFrame.style.width='0px';
                  tempIFrame.style.height='0px';
                  IFrameObj = document.body.appendChild(tempIFrame);
                  
                  if (document.frames)
                  {// this is for IE5 Mac, because it will only allow access to the document object
                  // of the IFrame if we access it through the document.frames array
                        IFrameObj = document.frames['RSIFrame'];
                  }
            }
            catch(exception)
            {// This is for IE5 PC, which doesn't allow dynamic creation/manipulation of iframe object.
                  // Instead, we'll fake it up by creating our own objects.
                  iframeHTML='\<iframe id="RSIFrame" style="';
                  iframeHTML+='border:0px;';
                  iframeHTML+='width:0px;';
                  iframeHTML+='height:0px;';
                  iframeHTML+='"><\/iframe>';
                  document.body.innerHTML+=iframeHTML;
                  IFrameObj = new Object();
                  IFrameObj.document = new Object();
                  IFrameObj.document.location = new Object();
                  IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
                  IFrameObj.document.location.replace = function(location) {this.iframe.src = location;}
            }
      }            
 
      if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument)
      {      // we have to give NS6 a fraction of a second
             // to recognize the new IFrame
             setTimeout('callToServer()',10);
             return false;
      }
 
      if (IFrameObj.contentDocument)
      {      // For NS6
            IFrameDoc = IFrameObj.contentDocument;
      }
      else if (IFrameObj.contentWindow)
      {      // For IE5.5 and IE6
            IFrameDoc = IFrameObj.contentWindow.document;
      }
      else if (IFrameObj.document)
      {      // For IE5
             IFrameDoc = IFrameObj.document;
      }
      else
      {
            return true;
      }
 
      IFrameDoc.location.replace(URL);
      return false;
}
//*****************END FILE***********************


(Watch out for some wrapping (due to forum) of above that should be on one line....)

That page always stays the same.

Then on the page that will do the call it will look something like this :


<!--- HTML/CF page with the form the user fills out --->
<html>
<head>
<title>Background Submit Example Page</title>
<script type="text/javascript" language="JavaScript" src="scripts/iframeRPC.js"></script>
<script type="text/javascript" language="JavaScript">
function LookupISBN(i)
{
      var ISBN = eval("document.TextbookAdoptionForm.ISBN_" + i).value;
            
      if (ISBN != "")
      {      // If an ISBN was entered then look it up in our DB
            callToServer("RPC_TextbookLookup.cfm?ISBN=" + escape(ISBN) + "&ID=" + i);
      }
}

function ISBNLookupComplete(ID, Found, Title, Author, Publisher, Edition)
{      
      if(Found)
      {      // ISBN in DB so fill in data
            eval("document.TextbookAdoptionForm.Title_" + ID).value = Title;
            eval("document.TextbookAdoptionForm.Author_" + ID).value = Author;
            eval("document.TextbookAdoptionForm.Publisher_" + ID).value = Publisher;
            eval("document.TextbookAdoptionForm.Edition_" + ID).value = Edition;
      }
      return;
}
</script>
</head>
<body>
<form name="myform" method="post" action="destination.cfm">
      ISBN:<input type="text" name="ISBN_1" id="ISBN_1" onBlur="LookupISBN(#i#);">
      Title:<input type="text" name="Title_1" id="Title_1" size="50" maxlength="60">
      Author:<input type="text" name="Author_1" id="Author_1" size="50" maxlength="60">
      Publisher:<input type="text" name="Publisher_1" id="Publisher_1" size="50" maxlength="60">
      Edition:<input type="text" name="Edition_1" id="Edition_1" size="50" maxlength="60">
etc...
</form>
</body>
</html>

Note that the action of the form is where you want the final form submitted to - not the background submit that will fill in the rest of the fields, but the page that will store the final submitted results (auto filled in fields AND user filled in fields)

Also note that the only field that has the onBlur event (this could also be onChange or onClick, etc) is the ISBN field or in your case the radio buttons.

The javascript include inthe head of the page includes the above iFrame file.
The two functions in the head of the page work as follows:

The LookupISBN function is called on the event such as onBlur.  It does a submit to the page I called RPC_TextbookLookup.cfm which has any variables passed in the URL.  I pass the ISBN and the ID of the field (since I actually have a dymnamic set of fields so that more than one textbook can be done at a time)

The code on the RPC_TextbookLookup.cfm page looks like this :

<cfset CleanISBN = REReplace(Ucase(URL.ISBN), "[^0-9,X,B]", "", "ALL")>
<cfquery datasource="myDSN" name="LookupTextbook">
      SELECT Title, Author, Publisher, Edition
      FROM mytable
      WHERE ISBN = <cfqueryparam cfsqltype="cf_sql_varchar" value="#CleanISBN#">
</cfquery>

<cfif LookupTextbook.RecordCount EQ 0>
      <cfset Found = false><!--- Did not find Textbook; set other fields to blank --->
      <cfset Title = "">
      <cfset Author = "">
      <cfset Publisher = "">
      <cfset Edition = "">
<cfelse>
      <cfset Found = true><!--- Found Textbook; set other fields to DB values --->
      <cfset Title = LookupTextbook.Title>
      <cfset Author = LookupTextbook.Author>
      <cfset Publisher = LookupTextbook.Publisher>
      <cfset Edition = LookupTextbook.Edition>
</cfif>

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<cfoutput>
<body onLoad="window.parent.ISBNLookupComplete(#URL.ID#, #Found#, '#Title#', '#Author#', '#Publisher#', '#Edition#');">
</body>
</cfoutput>
</html>

SO as soon as this page is called the CF runs and when the page is done loading the Javascript function runs.  The javascript line calls the 2nd function that was in the head of the html or cf page with the form and that populates the fields with the data if it was found.

Let me know if you have any questions and I can explain further.

 

by: mrichmonPosted on 2004-03-29 at 10:03:14ID: 10706191

The iFrameRPC.js file never changes for any pages that you ever want to use theis background submit.

The HTML page simply has your form, a function that gets called on the user action, and the function that fills in the return results.  So the 2 Javascript functions in the head and the form are the only things that need to be modified.

The last CF page - which is really short - only needs to have the query modified to pull your results (notice the parameters get passed via URL - but as this is done in the background the user never sees the URL so it can be as messy as necessary).  Then the call to the last javascript function back on the main page is how you pass back the data.

SO really there are 3 pages involved - but only 2 of those need to be modified.

 

by: anandkpPosted on 2004-03-29 at 23:50:28ID: 10711449

How abt having a cfm page in one of the frame - that is just sized 1x1 pixels.

so once the user fills in the details - this page inside the frame can do the necessary processing for u !
It cld then refresh the other page inside ur frame window to show the relavent matches.

let me know ...

K'Rgds
Anand

 

by: mrichmonPosted on 2004-03-30 at 08:17:27ID: 10714722

The point was to do a submit WITHOUT refreshing the page.  There are a lot of methods that work if you refresh the page, but this is one of the only ways to do it without refreshing the page.

 

by: n4nazimPosted on 2004-04-01 at 23:35:49ID: 10738920

Hi,

Here is what u can do ...

The page wld consist of two frames.

Left Frame: This would be hidden ( say keep width of this as zero ). This frame will consists of all the processing which is required to display the page. This would not contain ANY HTML interface.

RightFrame: This would purely contain the HTML interface with some division's. When the page is loaded, the Right frame ( only being HTML ) loads very fast giving an idea that the page loaded quicky. U can show the indicator GIF file saying "Processing ... Please wait .." or something like that. When the Left frame processing is finished, it would update the Right frame with proper values.

Hope this is what u require ...
Rgds,
Nazim M

 

by: trainmomPosted on 2004-04-03 at 17:05:49ID: 10750203

mrichmon.

Sorry it has taken me so long to test this out, but your explanations were so Great and logical...
However, when I (today) tried to make it work... the first error is in this line

 ISBN:<input type="text" name="ISBN_1" id="ISBN_1" onBlur="LookupISBN(#i#);">

It burps on the # sign.

I removed the # sign... and now I don't exactly know what we are passing with the i variable.
Thanks
Carol

 

by: trainmomPosted on 2004-04-03 at 17:12:39ID: 10750221

mrichmon,
one more thing, i set up a test db exactly like yours (ISBN, author, publisher, etc) to test this out
and changed the references
from:
eval("document.TextbookAdoptionForm.Author_" + ID).value = Author;
to
eval("document.myform.Author_" + ID).value = Author;

Can you explan a little more about passing the i variable in this
stagement
ISBN:<input type="text" name="ISBN_1" id="ISBN_1" onBlur="LookupISBN(#i#);">

Thanks.... I know this will work with a little more tweaking....
Carol

 

by: trainmomPosted on 2004-04-04 at 09:43:57ID: 10752437

ok, after some sleep, I now  think that the #i# is part of some cflooping and cfoutput functions that you do since you have dynamic fields.

trainmom

 

by: mrichmonPosted on 2004-04-05 at 08:08:58ID: 10757960

Yep  the passing in of #i# lets the javascript know which field to write the results back to since there could be any number of fields with the prefix of "Author_"

 

by: PaulioPosted on 2004-04-24 at 17:06:25ID: 10909914

Well, I'm seeing a lot of code for something that I think might be pretty simple [correct me if I'm wrong]
I think this might be the/a solution:

##### begin html page #####
<html><head><title>test color selection</title></head>
<body>
<!---// iframe were color will be sent to, and form values will be filled in from //--->
<iframe src="about:blank" id="hiddenframe" width=1 height=1 frameborder=1 style="display:none"></iframe>
<!---// script that submits the color via query string to processing page //--->
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
  function fillform(colorvalue){
    document.frames['hiddenframe'].location.replace('fill_colorform.cfm?color='+colorvalue);
  }
//-->
</SCRIPT>
<!---// example form: the onClick fires the js action //--->
<form action="somepage.cfm" name="colorform">
  choose color:<br>
  <input type="radio" name="color" id="color1" value="FF0000" onClick="fillform(this.value)">
    <label for=color1>Red</label><br>
  <input type="radio" name="color" id="color2" value="00FF00" onClick="fillform(this.value)">
    <label for=color2>Green</label><br>
  <input type="radio" name="color" id="color3" value="0000FF" onClick="fillform(this.value)">
    <label for=color3>Blue</label><br>
  Some form values to be filled in:<br>
  <input type=text name=fillme1 size=10><br>
  <input type=text name=fillme2 size=10><br>
</form>
</body></html>
###### end html page ####

### begin coldfusion page 'fill_colorform.cfm' #####
<!---// if a color is passed to this page //--->
<cfif IsDefined("url.color")>
  <!---// you can do some checking for valid hex color... //--->
  <!---// query the DB //--->
  <cfquery name="findcolor" datasource="yourDB">
    SELECT name, description
    FROM colors
    WHERE color = '#url.color#';
  </cfquery>
  <!---// output the results in js, so the (parent) form will be filled
  In this example, I assume there will be only one record found...
  (if more are found, only the last record will fill the form) //--->
  <cfoutput query="findcolor">
    <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
    <!--
      if(!parent){ alert('geen parent'); }
      else if (!parent.document){ alert('geen parent.document'); }
      else if (!parent.document.colorform){ alert('geen parent.document.colorform'); }
      var theform = parent.document.colorform;
      theform.fillme1.value = '#findcolor.name#';
      theform.fillme2.value = '#findcolor.description#';
      // etc.
    //-->
    </SCRIPT>
  </cfoutput>
</cfif>

<!---// if no color is passed to this form, or no records were found, we will clear the form //--->
<cfif NOT IsDefined("url.color") OR findcolor.recordcount EQ 0>
  <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
  <!--
    var theform = parent.document.colorform;
    theform.fillme1.value = '';
    theform.fillme2.value = '';
    // etc.
  //-->
  </SCRIPT>
</cfif>
###### end coldfusion page ######

#### tabel designed to work with example ####
table: colors,
columns: color,name, description (all text)
####

Hope this is the solution you were waiting for...

 

by: PaulioPosted on 2004-04-24 at 17:10:34ID: 10909921

oops...

I left in some checks which shouldn't be there:

In the example cf page, the first js block, leave out these first three sentences:

      if(!parent){ alert('geen parent'); }
      else if (!parent.document){ alert('geen parent.document'); }
      else if (!parent.document.colorform){ alert('geen parent.document.colorform'); }

(Not that they will do harm)

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...