homepage Welcome to WebmasterWorld Guest from 54.204.128.190
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Populating form field values with values of other form fields
tongpo




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

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.

 

Sekka




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

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.

astupidname




msg:3772192
 5:20 pm on Oct 23, 2008 (gmt 0)

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!

tongpo




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved