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

    Join Date
    Jun 2004
    Location
    Pakistan
    Posts
    82
    Rep Power
    11

    copying values from one form to another form using javascript !


    Hello,

    I have two forms on one page.
    Code:
    	<!-- Form 1 -->
    	<form action='test.php' method='post' name='form2' id='form2'>
    	<input type="text" name="name" value=''>
    	<input type="text" name="complete_address" value=''>
    	<input type="submit" value="Submit">
    	<input type="buton" value="Copy to Form 2">
    	</form>
    	
    	<!-- Form 2 -->
    	<form action='test.php' method='post' name='form2' id='form2'>
    	<input type="text" name="name1" value=''>
    	<input type="text" name="complete_address1" value=''>
    	<input type="submit" value="Submit"> 
    	</form>
    What I want to do is that, when I fill the first form & I click on a "Copy to Form2", it should copy the values of Form 1 to Form 2.

    I tried to use the following code
    Code:
    	<script language="javascript">
    	function copy() {
    		document.form2.name1.value = document.form1.name.value;
    		document.form2.complete_address1.value = document.form1.complete_address.value;
    		} 
    	</scirpt>
    I couldn't get it done using the above code. Any help in this regard would be highly appreciated.

    Regards,

    -Meraj
  2. #2
  3. Resident DJ
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    283
    Rep Power
    14
    It would probably work if the forms had different names.

    both are named & have ids of form2

    fix that issue and it should work.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Pakistan
    Posts
    82
    Rep Power
    11
    You were right. I did the following changes & it just worked.

    Code:
    <html>
    <head>
    <title> Test Form </title>
    <script language="javascript">
    function copy() {
    	document.form2.name1.value = document.form1.name.value;
    	document.form2.complete_address1.value = document.form1.complete_address.value;
    	} 
    </script>
    </head>
    <body>
    	<!-- Form 1 -->
    	<form action='test.php' method='post' name='form1' id='form1'>
    	<input type="text" name="name" value=''>
    	<input type="text" name="complete_address" value=''>
    	<input type="submit" value="Submit">	
    	</form> <br />	
    	<a href="javascript:void(0)" onclick="javascript:copy()"> copy </a>
    	<!-- Form 2 -->
    	<form action='test.php' method='post' name='form2' id='form2'>
    	<input type="text" name="name1" value=''>
    	<input type="text" name="complete_address1" value=''>
    	<input type="submit" value="Submit"> 
    	</form>
    </body>
    </html>
    Now one more question, If I use the the above script for any hidden values in any form to be copied in any visible field ? is it possible ?

    Regards,

    -Meraj

IMN logo majestic logo threadwatch logo seochat tools logo