Welcome to WebmasterWorld Guest from 54.146.174.220

Forum Moderators: coopster & jatar k

Populating form field values with values of other form fields

   
7:48 am on Oct 23, 2008 (gmt 0)

10+ Year Member



Not sure if this is the right section but seen as Im coding my pages using php thought I would post here.

Basically I am trying to find out how to populate form field values with data from other form field values. For instance I have come across many forms where I filled in my name and address etc as my contact details then say under administrator details it asks for similar information again with a button/tickbox that you can choose that says something along the lines of 'tick here if these details are the same as above' and once you do all the data you entered previously is inserted. I cant find any information on how to do this. Does anybody know or can anyone point me in the right direction.

8:24 am on Oct 23, 2008 (gmt 0)

10+ Year Member



At a guess, the data from the previous form will be placed in hidden fields. When you check this box, JavaScript will copy the information from these hidden fields into the visible ones.
5:20 pm on Oct 23, 2008 (gmt 0)

5+ Year Member



Hi tongpo, Sekka is right javascript would be the tool. I am not very experienced with php yet so I don't know if there is any way to do it in that. Using javascript (I am fairly handy with that!) it would not actually require hidden fields though. You can simply have the javascript copy the value of the already filled in input in to another one (which is what you would have to do to a hidden input anyways -but no need for the hidden one here). Here is a simple test case in javascript I have prepared which should point you in the direction you want (put script in head):

<script type="text/javascript">
/*<![CDATA[*/ /*for valid xhtml*/
window.onload = function(){document.forms.addressForm.reset();}/*** Resets form if page refreshed ***/

function duplicate()
{
var ship = document.getElementById("shipAddressBox");
if (ship.value.length > 0) /*** check the shipping address has been filled in (length > 0)***/
{
document.getElementById("billAddressBox").value = ship.value; /*** fills the billing address box in with the shipping address box's value ***/
}
else
{
alert("Sorry, you must fill in your shipping address first!");
document.getElementById("yes").checked = false; /*** unchecks the radio input ***/
}
}
/*]]>*/
</script>

<form action="" name="addressForm">
<p>Please enter shipping address:<input type="text" id="shipAddressBox" /><br />
Billing address same as shipping address?<br />
(must check one) &emsp;&emsp;Yes:<input type="radio" id="yes" name="confirm" onclick="duplicate();" />
&emsp;&emsp;No:<input type="radio" id="no" name="confirm" onclick="this.form.billAddressBox.value='';" /><br />
Please enter billing address:<input type="text" id="billAddressBox" name="billAdd" /><br />
<input type="reset" /></p></form>

Of course this is a simplified form and script. You would probably want to also verify onsubmit that one of the radio inputs has been checked also, and do more checking of the inputs values. But this is just an example, hope it helps!

11:38 am on Oct 24, 2008 (gmt 0)

10+ Year Member



Thanks a lot. That has helped a great deal :0)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month