Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post

    Question Autofill a form onclick of a button (fill the form)

    Hi to all the webmasters.....

    I am new to this forum and this is my 1st post, please let me know if you dont understand my requirement.

    My question is : I want to fill a form when someone clicks on the single button something like fill the form...

    The basic structure is... user logins and then at the start up he has been asked to fill a basic form and whatever the details he enters will be stored in session...

    I want these details to be filled up in "n" number of forms one by one on the click of a button..

    I think this makes some sense and I trust this will be possible to implement using PHP or Javascript or Ajax...

    I need some suggestions on how to implement this...

    Any help is greatly appreciated....

    Many thanks in advance...

  • #2
    New Coder
    Join Date
    Jan 2010
    Location
    UT
    Posts
    35
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Welcome to Coding Forums.

    Perhaps these snippets of code can shed some light on your idea.

    Here is some sample code of how to have your visitor store the values in the session:

    save.php
    Code:
    <?php
        session_start();
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            $name = $_POST["name"];
            $email = $_POST["email"];
            $_SESSION["name"] = $name;
            $_SESSION["email"] = $email;
    
            echo "Saved. <a href=\"forms.php\">Go to the forms</a>.";
        } else {
    ?>
    <form action="save.php" method="post">
    Name: <input type="text" name="name" value="<?=((isset($_SESSION["name"]))?$_SESSION["name"]:"")?>" /><br />
    Email: <input type="text" name="email" value="<?=((isset($_SESSION["email"]))?$_SESSION["email"]:"")?>" /><br />
    <input type="submit" value="Save" />
    </form>
    <?php
        }
    ?>

    Here is some sample code for your page that has the forms on it:

    forms.php
    Code:
    <?php
        session_start();
    ?>
    <script language="JavaScript">
    function fill_form(the_form) {
        document.the_form.name.value = "<?=$_SESSION["name"]?>";
        document.the_form.email.value = "<?=$_SESSION["email"]?>";
    }
    </script>
    
    <form name="form1">
    Name: <input type="text" name="name" /><br />
    Email: <input type="text" name="email" /><br />
    <input type="button" value="Fill In Form 1" onclick="javascript:fill_form('form1');" />
    </form>
    
    <form name="form2">
    Name: <input type="text" name="name" /><br />
    Email: <input type="text" name="email" /><br />
    <input type="button" value="Fill In Form 2" onclick="javascript:fill_form('form2');" />
    </form>
    Hopefully this is close to what you were looking for.
    Just here to help.

  • #3
    New Coder
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    Thanks Zoic,

    Exactly, This is close to what I am looking for... Thanks for you valuable reply..

    A small change in this (I forgot to mention)

    The fill button is not in the same form where the user fills up the details. Form is in iframe and the button is outside the frame...


    How can I do the autofill in this case??

    Thanks in advance..

  • #4
    New Coder
    Join Date
    Jan 2010
    Location
    UT
    Posts
    35
    Thanks
    1
    Thanked 3 Times in 3 Posts
    I'm not the best when it comes to dealing with frames (its kind of a thing of the past too), but try changing the javascript code to this:

    Code:
    parent.frame_name.the_form.name.value = "<?=$_SESSION["name"]?>";
    ...where frame_name is the name of the frame that the forms are located in.

    Its gotta be something like that...
    Just here to help.

  • #5
    New Coder
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    ok.. thanks..

    but the form name is dynamic. there will be "n" no of forms in an iframe (opens by clicking on "n" no of links in another frame). So I cannot specify the form name. I need to get the particular field name to be filled, since the form fields also not static, they may change...

    Is this possible or am I trying to do something not at all possible??

  • #6
    New Coder
    Join Date
    Jan 2010
    Location
    UT
    Posts
    35
    Thanks
    1
    Thanked 3 Times in 3 Posts
    You can actually get all of the forms on a page with javascript by using document.forms[0], document.forms[1], document.forms[2], etc. You won't have to worry about getting the name.

    Here is a tutorial that will get more in-depth about them.

    http://www.quirksmode.org/js/forms.html
    Just here to help.

  • #7
    New Coder
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    Thats great,,,

    but I am not getting the box filled with the value using

    parent.frame_name.the_form.name.value = "<?=$_SESSION["name"]?>";


  • #8
    New Coder
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    Without using forms can I use something like this

    Instead of this
    parent.framename.formsubmit.txtbox.value

    Can I use this...
    parent.framename.getElementById("txtbox").value --------- Is this valid??

  • #9
    New Coder
    Join Date
    Jan 2010
    Location
    UT
    Posts
    35
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by shanu.chaudhary View Post
    Without using forms can I use something like this

    Instead of this
    parent.framename.formsubmit.txtbox.value

    Can I use this...
    parent.framename.getElementById("txtbox").value --------- Is this valid??

    window.frames[0].document.getElementById("txtbox").value should work. The "txtbox" needs id="txtbox" though.
    Just here to help.

  • #10
    New Coder
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    ok.. but Id is not alwayz present in all the forms..So In this case I think I need to change the value using "name"
    How can I do this from an iframe??


  •  

    Posting Permissions

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