/    Sign up×
Community /Pin to ProfileBookmark

Posting form in IFrame from main page not working right

I have an IFrame that contains a form and a link on the main page to submit the form in the IFrame. If I use a submit button in the form in the IFrame, then the form submits properly. It does not work properly if I try to submit with JavaScript from the main page. The form in the IFrame posts back to the same page. I have supplied some code snippets below. They are not complete. Just examples to show the problem.

You can see there is an alert to display the contents of notes_1 on the form submit within the IFrame. There is also an alert at the end of the IFrame page to show the contents of note_1 upon load. If I change the contents of notes_1 and press the submit button in the IFrame, the first alert will show me the current content of notes_1 (the changed value). Upon reloading, the alert shows the contents of notes_1 to be the original value from the db because I’m not actually saving changes yet.

You see that all the Save link does on the main page is click the same submit button I pushed manually above. But when I do this the alert shows me the OLD value of notes_1 before I typed in something else. The alert on the bottom of the IFrame page happens showing that the page has reloaded, but IT DOESN’T REFRESH THE IFRAME. The IFrame continues to show what I typed into the field before pressing the Save link.

Two things I do not understand: 1) Why does posting from the main page give me an alert with the old value? and 2) why does the IFrame not refresh upon the post? The post was successful but the IFrame doesn’t show so.

This is HTTPS if that makes any difference.

Thanks,
Kirt

<!– This is the main doc snippet –>
<fieldset>
<legend>&nbsp;Emergency Contacts&nbsp;&nbsp;<a href=”#” onclick=”SaveEmergencyContacts(); return false;”>[Save]</a>&nbsp;</legend>
<iframe SRC=”EmergencyContacts.php” name=”ECIframe” id=”ECIframe” width=”622″ height=”210″ frameborder=”0″></iframe>
</fieldset>

<script>
function SaveEmergencyContacts()

{
document.frames[‘ECIframe’].document.getElementById(‘test’).click();
}
</script>

<!– This is the IFrame snippet –>
<body leftmargin=”0″ topmargin=”0″ marginwidth=”0″ marginheight=”0″>
<form action=”EmergencyContacts.php” method=”post” name=”ECForm” id=”ECForm”
onsubmit=”alert(document.getElementById(‘notes_1’).value);”>
<input type”text” name=”notes_1″ id=”notes_1″ value=”This is a test”>
<input type=”submit” name=”test” id=”test” value=”Click Me”>
</form>

<script>
alert(document.getElementById(‘notes_1’).value);
</script>
</body>
</html>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@so_is_thisNov 30.2006 — No cross-domain issues here, I hope. As for https, are both documents using https?

Instead of this:

document.frames['ECIframe'].document.getElementById('test').click();

have you tried this?

document.frames['ECIframe'].document.forms['ECForm'].elements['test'].click();

Otherwise, I'd advise not using the word "test" to name your things as this is a word already known to JavaScript and confusion can occur.
Copy linkTweet thisAlerts:
@KirtcauthorDec 06.2006 — Thanks for the reply. I have tracked it down. I inherited this app from someone else. The page in question is using a tabbed interface. The form displayed upon clicking a particular page is actually copied (using innerHTML) into a div for that tab from a hidden div on the page that holds all possible forms for the tabbed interface. Not sure if that makes much sense. The bottom line is that this "copying" creates two instances of the form objects on the page and that confuses JS. I rewrote that code to ensure there is never more than one instance of a particular form on the page and now everything seems to work.
×

Success!

Help @Kirtc 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 4.30,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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