#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    15

    Unhappy auto populating form


    Hi:

    I have a form that has "lastname" and "firstname" input boxes. Further down in the form I have another input box that asks for "form submitted by". I'd like to have a checkbox next to it which the user can check if the individual submitting the form is the same person as the lastname,firstname fields and so when the user checks the checkbox the "form submitted by" input box gets automatically populated by the content in the firstname,lastname input boxes. The form does not submit until after many other fields are filled after the "form submitted by" field. How do I do this?

    An example of what I want:

    https://ww1.vibef9.com/booknow/Booki...0%3A51%3A44+AM

    If you fill in the firstname and lastname fields in the form and then go down to "person making reservation" and check the checkbox, the field gets auto filled with the firstname lastname that you entered above.

    How do I do this in php? I wouldn't mind using javascript either.

    Thank you very much for your time.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    62
    Rep Power
    15
    Javascript solution:

    <form onSubmit="return populate_fields()">

    function populate_fields(){

    // set form reference
    var d = document.forms[0];

    // concatenate firstname & last name strings
    d.form.form_submitted_by.value = d.firstname.value + ' ' + d.lastname.value;

    return true;

    }


    PHP solution:

    $f = $_POST;

    $firstname = $f['firstname'];
    $lastname = $f['lastname'];
    $form_submitted_by = $f['firstname'].' '.$f['lastname'];

    I hope that's what you meant cause it took me 1 minute to write.

    Cheers,

    Steve
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    15
    Looking at the php code, it looks like the "form submitted by" text box will always be populated by the "firstname" and "lastname" text box contents. I was hoping to use a checkbox to fill the "form submitted by" field if the person submitting the form is the same as the name in the "firstname" "lastname" field. I understand that its possible by using the onClick() method in Javascript but don't really know how to do it in php.

    I hope that makes sense. I guess my problem is akin to websites where they auto fill an address for you if maybe your billing address is the same as your shipping address instead of asking you to type your address twice.

    Thanks for your help steve.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    62
    Rep Power
    15
    onClick="populate_fields()"

    add to function:

    if(d.checkbox_name.checked){

    }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    15
    I ended up doing this:

    <script language="JavaScript" type="text/javascript">
    function populate_fields()
    {
    if (document.form1.check.checked)
    {
    if (document.forms['form1'].fname.length == undefined)
    {
    document.forms['form1'].ReceivedFromName.value=document.forms['form1'].fname.value + " " + document.forms['form1'].lname.value;
    }
    else
    {
    document.forms['form1'].ReceivedFromName.value=document.forms['form1'].fname[0].value + " " + document.forms['form1'].lname[0].value;
    }
    }
    }
    </script>
    and calling the function using onclick="populate_fields() from "form submitted by" input type=checkbox code.

    Thanks for all your help.

IMN logo majestic logo threadwatch logo seochat tools logo