fanfavorite Posted September 15, 2008 Share Posted September 15, 2008 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. Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 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. Quote Link to comment Share on other sites More sharing options...
fanfavorite Posted September 17, 2008 Author Share Posted September 17, 2008 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. Quote Link to comment Share on other sites More sharing options...
fanfavorite Posted September 17, 2008 Author Share Posted September 17, 2008 I added an id field and then changed it to if (document.getElementById('firstname'+i)){ fn = document.getElementById('firstname'+i).value; url=url+"&fn"+i+"="+fn; } Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 Is that working for you, or are you still getting undefined? Quote Link to comment Share on other sites More sharing options...
fanfavorite Posted September 26, 2008 Author Share Posted September 26, 2008 It's working for me. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.