www.webdeveloper.com
Results 1 to 7 of 7

Thread: JavaScript iFrames and Forms

  1. #1
    Join Date
    Jan 2009
    Posts
    4

    Question 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?

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    index
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <script type="text/javascript">
    function autoFill() {
    var f=if1.document.forms[0];
    f.txtName.value='Zanne';
    f.submit();
    }
    </script>
    
    </head>
    <body>
    <div>
    <button type="button" onclick="autoFill();">autoFill</button>
    </div>
    <iframe name="if1" id="if1" src="form.php" width="100&#37;" height="100%"></iframe>
    </body>
    </html>
    form
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    </head>
    <body>
    <?php
    if($_POST['txtName']) {
        echo "Your name is " . $_POST['txtName'];
        exit;
        }
    
    echo <<<HDOC
    <form method="post" action="form.php">
    <div>
    <input type="text" name="txtName">
    <input type="submit" value="Submit">
    </div>
    </form>
    HDOC;
    ?>
    </body>
    </html>

  3. #3
    Join Date
    Jan 2009
    Posts
    4
    Now, is there a way to do this without using the button?

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Using what?

  5. #5
    Join Date
    Jan 2009
    Posts
    4
    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.

  6. #6
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    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.

  7. #7
    Join Date
    Jan 2009
    Posts
    4
    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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles