/    Sign up×
Community /Pin to ProfileBookmark

submit form data to parent window

I’m trying to figure out how when the “submit” button is pressed, in the window that has popped up from clicking on a link in the original window, to get it to close the popped up window and send the results of the form submit to the parent window.

the page in the original browser window would have a half dozen buttons that would link to different preformatted windows that would popup with different form choices. Basically it is an “add to cart” feature that on some items would popup a html window with things like color choices etc. in it. Then when they click on the add-to-cart that matches the color/model they want it would close the new window and submit the data so that the cart would show in the original window.

Is this possible?

-Trevor

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@7studJan 28.2005 — To take some action when a user submits a form, you put an onsubmit event handler in the <form> tag. Your onsubmit even handler will then call a function.

<form name="f" method="post" action="default.htm" [color="red"]onsubmit="return send_to_main()"[/color]>

Since a submit button sends form data off to the server, you are going to want to cancel the submit, which can be done by returning 'false' from the function.

In send_to_main(), you can get the values of the user's choices in the popup, then add that information to the main page:

opener.color = document.getElementById("colorID").value;

where on the main page you have this:

var color;

In the popup, you can do anything with a script that you could do with a script on the main page--you just have to use 'opener' to reference the main page, e.g. opener.mainpage_function(), opener.mainpage_var.

If you want, you don't even need to have any functions on the popup page. You could change your onsubmit event handler to this:

<form name="f" method="post" action="default.htm" onsubmit="return [color="red"]opener[/color].get_popup_vals()">

From the function get_popup_vals() on the main page you can access the popup window, using the window reference returned by window.open();

var popup = window.open(.....);

<i>
</i>function get_popup_vals()
{
var color = popup.document.getElementById("colorID").value;
..
..
return false;
}



To close the popup:

window.close();

To cancel the submit:

return false;
×

Success!

Help @tallen240 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...