tags:

views:

55

answers:

2

I have Parent page and a Child popup window page (empW.cfm). The Child page has search field, that searches and displays results. I have to select one search result from thre Child page and populate it on the Parent page.

I am unable to pass the correct selected value from the Child to Parent page.

Parent page:-

    <script type="text/javascript"  >

    function doSubmit() {
      var Emp = document.getElementById("emp");
      var getName = document.getElementById("getName");
      Emp.value = getName.value;
                }
           </script> 
</head>
<body>
<cfajaximport tags="cfform,cfwindow" scriptsrc="test.js">
<cfform action="Action.cfm" name="formE" id="formE" preserveData="true"  enctype="multipart/form-data" method="post" onsubmit="return validate(document.formE);"  >  
 <table >
    <tbody>    
            <tr><td  > Name*: </td><td><cfinput name="Name" id="Name"  type="text"  ></td></tr>          

             <tr><td > EMP:</td>
<td><input name="searchName" id="emp" onclick="ColdFusion.Window.create('w1','Title','empW.cfm')"></td>

    </tr>  
   </tbody>
 </table>
</cfform>

Child/Window page:-

   <cfform name="formI" id="formI" preserveData="false" method="post">
 <table>
 <tr>SERACH:-  <input name="getName" id="getName" type="text" value="Find emp name" >
<cfif isdefined('form.getName')>
<tr>
<cfloop startrow="1" endrow="qry" query="qry">
   <cfoutput>Selected = #qry.getName# 
 <input name="Add" id="getNm" type="button" value="submit" onclick="document.getElementById('emp').value=document.getElementById('getName').value;">
</cfoutput>
</ cfloop>
tr>

</cfif>
 </table>
</cfform>  

Example:- there are 10 search results on the Child page, and I select the 8th result , still the Parent page is populated by the 1st result only. This is the case when I select everytime I select an search result.. When I click Submit button on the Child , only the First value of the Search result is being passed.

How to pass the exact selected search result, from the Child to the Parent page.

Please help.

+1  A: 

Two things. You should remove the src from your javascript tag. And move that to a second set of script tags. Your "getName" fields need a unique value. Right now you will end up with multiple getName fields with the same ID. Also, on a side note, you don't need to import cfform if you are using it.

Jason Tabler
"Right now you will end up with multiple getName fields with the same ID. "
Fransis
yes that is what i am getting right now Jason, where as i want to pass only the Selected value.
Fransis
i have corrected js/"src" and have an unique value for getName. stil
Fransis
So, you have your search field with an ID of getNameThen you have your buttons with ID getNameYou've made all of these unique and updated your javascript on the onClick?
Jason Tabler
i changed the ID names and rmoved "src" from " <script type="text/javascript" src="test.js" >" add added to <cfajaximport csssrc"test.js">....but the JS is not being called
Fransis
so to answer you Q I was able to do only one of the 2 items that is change ID
Fransis
Could you update your code in the Q?
Jason Tabler
yes i have updated by changng the ID and using scriptsrc="test.js"
Fransis
I still don't think you have unique IDs for all your fields. You will want to set your IDs to be id="getName#qry.currentRow#" or something like that. You'll also have to put that in the javascript - document.getElementById('emp').value=document.getElementById('getName#qry.currentRow#').value;That gives each input field a unique ID based off of the current row in loop of the query.
Jason Tabler
it worked 1 ) document.getElementById('emp').value=document.getElementById('getName#qry.currentRow#').value; and 2) by naming the <input Name="N1" and id="N11", i,e different.
Fransis
i have not removed the src in JavaScript, still it has worked .Can you please throw some light on this src issue?
Fransis
You are supposed to either use the script tag to link to an external script with the src attribute or include JS within the tags, but not both. That's the W3C definition at least. So, you'd just use two script tag pairs instead of one.
Jason Tabler
Glad it is working. Just keep in mind that you should never reuse the id field for any element on a page. It should always be unique. You can reuse the name attribute for things like radio groups, but the ID attribute is a unique thing.
Jason Tabler
+1  A: 

I figured out your problem, Let me explain your problem:

Just understand my simple code:

<html>
    <head>
        <script>
            function transform(){
                var x_elements = document.getElementsByName('x');
                var y_elements = document.getElementsByName('y');
                var i=0;
                for (i=0; i<x_elements.length; i++)
                {
                    y_elements[i].value = x_elements[i].value;
                }
            }
        </script>
    </head>
<body>
    <select name="x" id="x">
        <option value="1">one</option>
        <option value="2">two</option>
        <option value="3">three</option>
    </select>
    <select name="x" id="x">
        <option value="5">five</option>
        <option value="6">six</option>
        <option value="7">seven</option>
    </select>
    <br/>
    <select name="y" id="y">
        <option value="1">one</option>
        <option value="2">two</option>
        <option value="3">three</option>
    </select>
    <select name="y" id="y">
        <option value="5">five</option>
        <option value="6">six</option>
        <option value="7">seven</option>
    </select>
    <br/>
    <input type="button" value="Check" onclick="javascript:alert(document.getElementById('x').value);" />
    <br/>
    <input type="button" value="Transform" onclick="javascript:transform();" />
</body>
</html>

I've intentionally kept the same name and id to two select tag. While accessing the element by id, you will have access to only first element with same id (e.g. Check button). So you can use documents.getElementsByName().

At last I would still suggest that try to avoid this situation and keep id unique for each element. You can use loop and counter generate unique id. e.g. name1, name2 etc..

For quick and easy javascripting, use jQuery.

Vikas