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

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

  • 2 weeks later...
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.