Jump to content

Recommended Posts

if you have specific checkboxes that you want to shop up for specific checkboxes, you could have checkboxes within an element that is "display: none" and then use jquery .show() to show it if a specific checkbox is checked.

 

http://api.jquery.com/show/

Here is code I used to add additional 'Address' fields to a form:

function newaddress() {
formvalue = parseInt( document.getElementById( 'additions' ).value ) + 1;
formvalue = formvalue.toString();
formdata = document.getElementById( 'fields' );
checkfields = formdata.getElementsByTagName( 'input' );
totalstored = 0;
var inputname = new Array();
var inputvalu = new Array();
for( var i=0; i<checkfields.length; i++ ) {
	var testvalue = checkfields[ i ].value;
	if( testvalue.length > 0 ) {
		inputname[ totalstored ] = checkfields[ i ].id;
		inputvalu[ totalstored ] = testvalue;
		totalstored++;
	}
}
newform = '<fieldset><label for="fname' + formvalue + '">First Name</label><input name="fname';
newform += formvalue + '" id="fname' + formvalue + '" /><label for="lname';
newform += formvalue + '">Last Name</label><input name="lname' + formvalue;
newform += '" id="lname' + formvalue + '" /><label for="address' + formvalue;
newform += '">Address</label><input name="address' + formvalue + '" id="address';
newform += formvalue + '" /><label for="city' + formvalue + '">City</label><input name="city';
newform += formvalue + '" id="city' + formvalue + '" /><label for="state' + formvalue;
newform += '">State</label><input name="state' + formvalue + '" id="state' + formvalue;
newform += '" /><label for="zip' + formvalue + '">Zip Code</label><input name="zip';
newform += formvalue + '" id="zip' + formvalue + '" /><label for="phone' + formvalue;
newform += '">Phone Number</label><input name="phone' + formvalue + '" id="phone' + formvalue + '" /></fieldset>';
formdata.innerHTML += newform;
for( var i=0; i<totalstored; i++ ) {
	document.getElementById( inputname[ i ] ).value = inputvalu[ i ];
}
document.getElementById( 'additions' ).value = formvalue;
return false;
}

The form field that had the 'additions' Id was a hidden field. I appended numbers on the end of the field names so they had unique names for PHP. I found when I added additional fields, the previous fields reset to their default values, so I added the loops to store, then restore, the field values that were filled in before the additional fields were added. Perhaps you can modify this code to work for you.

you could use arrays rather than formvalue, that would allow you to do this without having to rebuild every time. it would also make it easier when you go to process the form.

 

by arrays i mean like:

 

<input name="fname[]" />

 

then when you process you could have

 

foreach ($_POST['fname'] as $fname) {

  //process each

}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.