Solved

pass selected form data to new window via Javascript

Posted on 2008-11-02
12
291 Views
Last Modified: 2013-11-19
I have a basic xhtml form with several check boxes/items to choose from.  I would like to open a new browser window and list ONLY the items that were chosen/selected on the form.  I would like to use Javascript if possible.  Anybody know how to do this?  Thanks!  
0
Comment
Question by:webbedone
  • 6
  • 6
12 Comments
 
LVL 18

Expert Comment

by:Pawel Witkowski
ID: 22863246
look at code I gave you. It just change URL in your browsers like:

http://somepage.net/index.html?param1=test¶m2=blabla

to something accesible from javascript:

params[0] = "test";
params[1] = "blabla";


And to get that kind of adress.. just use <form method="get"> so all variables should be sent in that format... Does that sounds ok ?:)
var table=location.search.replace(/^\?/,"").split("&"); 

var params=new Array(); 

for (var i=0,y=table.length;i<y;i++) params[i]=table[i].split("=");

Open in new window

0
 

Author Comment

by:webbedone
ID: 22863883
Here's what I have so far (see below).  I just need the results to display in a new window instead of on the same page.  I'm not sure how to get the selections to display in a new window.   Any ideas?  

Thanks!!
<html>

<head>

<script type="text/javascript">

function createOrder()

{

commod=document.forms[0].commod;

txt="";

for (i=0;i<commod.length;++ i)

  {

  if (commod[i].checked)

    {

    txt=txt + commod[i].value + " ";

    }

  }

document.getElementById("order").value="You chose the following commodities: " + txt;
 
 
 

}
 
 

</script>

</head>
 

<body>

<p>Which commodities would you like to choose?</p>

<form>

<input type="checkbox" name="commod" value="banana">banana<br />

<input type="checkbox" name="commod" value="cucumber">cucumber<br />

<br />

<input type="button" onclick="createOrder()" value="View Selection">
 

<br /><br />

<input type="text" id="order" size="100">

</form>

</body>
 

</html>

Open in new window

0
 
LVL 18

Expert Comment

by:Pawel Witkowski
ID: 22863956
Want to show this txt on other page?

then just make a link, or form submit... dunno - just go to the second page and run script that i gave you. The url should be

var url = "http://some.net/some.html?txt="+txt;

and put that url after:

document.getElementById("order").value="You chose the following commodities: " + txt;


for example you can go there directly by:

window.location=url;


then with script i gave you before you can read this txt param as:




params['txt']




There was a small mistake in mine first response:
params[0] = "test";
params[1] = "blabla";

this is bad, correct one is:

params['param1'] = "test";
params['param2'] = "blabla";
0
 

Author Comment

by:webbedone
ID: 22885573
wilq32-

 I still couldn't get it to work.  Thanks anyway for your help!!
0
 
LVL 18

Expert Comment

by:Pawel Witkowski
ID: 22886021
Sorry for beeing offline, can you precise what is not working ? For best javascript developement install firefox and firebug tool for it (alot of errors shown there)
0
 

Author Comment

by:webbedone
ID: 22886256
I got the new page to come up, but I couldn't get the selected items to display on the new page.  I added window.location as suggested to script on page1, and then added your script to the new page, but am not sure how to display selections. ??
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 18

Expert Comment

by:Pawel Witkowski
ID: 22886501
Oh well i'm almost sure that I dont know what you really want :) Show some example of functionality that you need
0
 

Author Comment

by:webbedone
ID: 22886880
ok - I have a form where a user can select items via checkboxes, when they hit submit button I need a new window to open and display the items that they selected on that form.  The code I have above displays their selected items on the same page, I need it to be displayed in new window.  I've attached my code below...
<html>

<head>

<script type="text/javascript">

function createOrder()

{

commod=document.forms[0].commod;

txt="";

for (i=0;i<commod.length;++ i)

  {

  if (commod[i].checked)

    {

    txt=txt + commod[i].value + " ";

    }

  }

document.getElementById("order").value="You chose the following commodities: " + txt;

window.location="http://localhost/jstestdis.htm?txt="+txt;
 
 

}
 
 

</script>

</head>
 

<body>

<p>Which commodities would you like to choose?</p>

<form>

<input type="checkbox" name="commod" value="banana">banana<br />

<input type="checkbox" name="commod" value="cucumber">cucumber<br />

<br />

<input type="button" onclick="createOrder()" value="View Selection">
 

<br /><br />

<input type="text" id="order" size="100">

</form>

</body>
 

</html>

Open in new window

0
 
LVL 18

Expert Comment

by:Pawel Witkowski
ID: 22893063
Again as i said before, on page http://localhost/jstestdis.htm put javscript that i gave you. This will read information that you put previously in window.location
<script type="text/javascript">

function parseParams()

{

var table=location.search.replace(/^\?/,"").split("&"); 

var params=new Array(); 

for (var i=0,y=table.length;i<y;i++) params[i]=table[i].split("=");

return params;

}
 

document.write("You have chosen the following commodities:"+parseParams()['txt']);
 

</script>

Open in new window

0
 

Author Comment

by:webbedone
ID: 22894610
Yes, Did that.  The first code snippet above was incomplete (only 3 lines).  I added the complete script above to jsdist.htm.  It does open the new window, and shows text "you have chosen..."  but says "undefined" where it should show the selection.  Oh well, don't worry about it.  Thanks again!!
0
 
LVL 18

Accepted Solution

by:
Pawel Witkowski earned 500 total points
ID: 22894697
mine fault, try this:


function parseParams()

{

var table=location.search.replace(/^\?/,"").split("&"); 

var params=new Array(); 

for (var i=0,y=table.length;i<y;i++) 

{

var temp=table[i].split("=");
 

params[temp[0]]=temp[1];

}

return params;

}

 

document.write("You have chosen the following commodities:"+parseParams()['txt']);

Open in new window

0
 

Author Comment

by:webbedone
ID: 22894784
Great!  It works!  Now I just have to figure out how to format the selections.  Thanks so much!  You've been a great help!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now