/    Sign up×
Community /Pin to ProfileBookmark

JavaScript iFrames and Forms

I first want to display the two files:

index.htm:
<iFrame name=”if1″ id=”if1″ src=”form.php” width=”100%” height=”100%”></iFrame>

form.php:
<?
if($txtName) {
echo(“Your name is $txtName”);
exit;
}

echo(“
<form method=post action=form.php>
<input type=text name=txtName>
<input type=submit value=Submit>
</form>
“);
?>

Here’s my question:
Basically, I have form.php in an iFrame. Is there any way I can use JavaScript in index.htm to fill out form information in form.php and submit the data? This would be a huge help if there is a way! I’ve researched many ways, but can’t find a way that actually works. Any ideas?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@FangJan 06.2009 — index&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;script type="text/javascript"&gt;
function autoFill() {
var f=if1.document.forms[0];
f.txtName.value='Zanne';
f.submit();
}
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;
&lt;button type="button" onclick="autoFill();"&gt;autoFill&lt;/button&gt;
&lt;/div&gt;
&lt;iframe name="if1" id="if1" src="form.php" width="100&amp;#37;" height="100%"&gt;&lt;/iframe&gt;
&lt;/body&gt;
&lt;/html&gt;

form&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;?php
if($_POST['txtName']) {
echo "Your name is " . $_POST['txtName'];
exit;
}

echo &lt;&lt;&lt;HDOC
&lt;form method="post" action="form.php"&gt;
&lt;div&gt;
&lt;input type="text" name="txtName"&gt;
&lt;input type="submit" value="Submit"&gt;
&lt;/div&gt;
&lt;/form&gt;
HDOC;
?&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@ZanneauthorJan 06.2009 — Now, is there a way to do this without using the button?
Copy linkTweet thisAlerts:
@FangJan 06.2009 — Using what?
Copy linkTweet thisAlerts:
@ZanneauthorJan 06.2009 — I know PHP and some JavaScript. I'd like to know how to do this preferabbly in PHP, but which ever is easier. Here's basically what I'm doing.

I'm pulling information off of my site, and submitting it to another site on a different domain. I load the other site in the iFrame and fill out the form through that. I'm basically setting up a cron job for this, so I don't have to do this 3AM at my time every day.

I have friends tell me there is a way to do this in just PHP, but I haven't found a way, so I just started using iFrames with JavaScript.
Copy linkTweet thisAlerts:
@FangJan 06.2009 — You can't use the method given; you are not allowed to reference elements on an external site.

You could try passing a URL with query (i.e. http://www.google.com/search?q=mozila+dom), but most, non-search, sites will block this.
Copy linkTweet thisAlerts:
@ZanneauthorJan 06.2009 — Bah, thank you Fang for trying =). I'll take a look at C#/C++ to see if I can use Browser Control or something.

~God Bless
×

Success!

Help @Zanne 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,
)...