PDA

View Full Version : New Window Open on post data



lupos
09-29-2008, 05:31 PM
When i press button i would like to post data and open posted data in new window. Here is my code

onclick="
window.open('new.html', 'name').focus();
document.forms['example'].submit();
"
but i get new window opened without posted data and in the page that i have pressed button i get posted data which should be in new window.
Could somebody correct my code?

jscheuer1
09-29-2008, 08:08 PM
You need to target the window from the form, and you may as well use a normal submit button, forget about onclick, and use the onsubmit of the form to open the new window. This may need some tweaking (like a timeout to give the window time to open before the form actually submits or moving the window open to an onmousedown of the submit button to open the window, also with the idea being to get the window open in time for the actual submission), but we will worry about that if there is a problem. So I would do something like:


<form action="new.html" target="submission"
onsubmit="window.open('',this.target);return true;" method="post">
Your Form Contents Here
<input type="submit" value="Submit">
</form>

new.html should be the name of the page you are posting to.

Added Later:

But this can be done without javascript:


<body>
<form action="new.html" target="_blank" method="post">
Your Form Contents Here
<input type="submit" value="Submit">
</form>

lupos
10-08-2008, 03:00 PM
Thx for help. It really works. But my script is now bigger and I've got another problem.
I've got menu using CSS sytle and it's outside the form.



<form action="new.html" target="_blank" method="post">
My Form Contents Here
</form>

// menu
<input type="submit" value="Submit">

I want to post some data using this menu and refresh site after post. How can I submit outside the form. I'm using java script but I got a problem. 1st time when I click it seems to doesn't work but after clicking second time it works fine. I'm posting my data and storing them into cookies.

jscheuer1
10-08-2008, 04:24 PM
As far as submitting the form from the menu goes, the easiest thing would be to include the menu in the form. It won't hurt anything if it is well planned out:


<form action="whatever" target="_blank" method="post">
Form contents Here

<!-- Menu -->

<input type="submit" value="Submit">

<!-- End Menu -->
</form>

If you want the form proper to have an outline or anything special as far as style goes, and have that be different than the menu, use something like:


<form action="whatever" target="_blank" method="post">

<div id="formstyles">
Form contents Here
</div>

<!-- Menu -->

<div id="menustyles">
Other Menu Stuff Here
<input type="submit" value="Submit">
Other Menu Stuff Here
</div>

<!-- End Menu -->
</form>

You can use whatever ids, or even classes if you like for the two divisions to get different styles in the two areas. But it will still be all one form. Care should be taken not to name any inputs in the menu section whose values you don't want to receive on post. But, as long as your server side code isn't looking for values from inputs with those names, even that shouldn't hurt anything. I only mention this in case you have other input elements in the menu area and need to use their name attributes for other purposes.

You can even have all sorts of stuff in between the 'end' of the form proper and the beginning of the menu, probably just not other forms.

Bottom line, as long as it's valid HTML, forms don't care how much other stuff is inside their opening and closing tags. Other (complete) forms may even be OK, I just wouldn't want to guarantee it.

lupos
10-09-2008, 06:48 PM
Is there any posibility to use html anchors to submit target or any other html element which can be override in css style?

jscheuer1
10-09-2008, 07:49 PM
Is there any posibility to use html anchors to submit target or any other html element which can be override in css style?

I don't see why you want to override css style, instead use it as you like building upon the second example from my last post.

The menu can be styled one way and the the area in the form you want set apart can be styled another. The form itself need not be used to style any of its contents - that's what the divisions are for.