Jump to content

[SOLVED] Repopulate Fields


fanfavorite

Recommended Posts

I have a form that asks for the select number of people:

 

 
                  		<div class="formsleft"># Of People:</div>
                   		<div class="formsright">
			<select name="personnum" onchange="showField(this.value,'personinfo')">
				<?
				for ($count = 1; $count <= 30; $count++) {
					echo '<option value="'.$count.'">'.$count.'</option>';
				}
				?>
			</select>
		</div>
                  		<div id="personinfo"></div>

 

Once a value is selected, it populates the personinfo div with form values:

 

	
for ($counter = 1;$counter <= $_GET["value"];$counter++){
?>
<div class="formsleft">Person <? echo $counter ?>:</div>
<div class="formsright"><input name="firstname<? echo $counter ?>" type="text" value="<? echo $_POST['firstname'.$counter] ?>" size="15" /></div>
<?	
}

 

This works great, but the problem is that when someone has entered anything in any fields and then changes the number of people, everything gets removed.  Here an example of the problem:

 

Scenario:

 

- User chooses 3 people

- User enters firstname1: Joe, firstname2: John, firstname3: Jamie

- User decides that they need another person, so chooses 4 people

- 4 fields show up, but all are blank

 

I need it to copy old firstname1 = new firstname1, etc.  How would I go about doing this? 

 

Thanks. 

Link to comment
https://forums.phpfreaks.com/topic/124330-solved-repopulate-fields/
Share on other sites

I would say that you need to add some script to your showField function that grabs any applicable value currently in the field, and passes it on to your PHP code to then populate your inputs with it.

 

So, in your shoeField function, add something like this (numfields would be your selected value):

 

var inputs = newArray();

var i;

for (i=1;i<=numfields;i++){

  if (document.getElementByName('firstname'+i)){

    input = document.getElementByName('firstname'+i).value;

  }

}

 

then pass the input variable to your AJAX code to repopulate the input fields.

Thanks F1Fan.  I think I am on the right track, but the input is coming out as undefined.   

 

function showField(value,div1) { 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
	alert ("Browser does not support HTTP Request");
 	return;
}

var url="https://www.anothertakestudios.com/js/change.php";
url=url+"?value="+value;
url=url+"&div1="+div1;

var i;
for (i=1;i<=value;i++){
	if (document.getElementsByName('firstname'+i)){
		fn = document.getElementsByName('firstname'+i).value;
		url=url+"&fn"+i+"="+fn;
	}
}

url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=function () {
	stateChange(div1);
};
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

 

Any ideas why? 

 

Thanks. 

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.