/    Sign up×
Community /Pin to ProfileBookmark

Keep form inputs when adding new fields

I’m new to javascript and I cannot work this out despite looking around a lot and trying to work through all the form functions etc that Javascript has.

I have a script (below) that works fine however i want to keep the data entered by the user so that when they add another session it just adds another set of times to the bottom, without loosing their already entered data.

If you know how to do this please let me know!

Thankyou sooo much for any help!

[CODE]<html>
<head>
<title>Dynamic Schedule Demonstration</title>

<script type=”text/javascript”>
//initialise the form variables
// letter multiples that applies
var oc_letter = ‘a’;
var yes_letter = ‘a’;
var no_letter = ‘b’;

var repititions_of_fields = 0;

//the string that is to be repeated
var core_string = “”;
core_string += “<td rowspan=’2′>For all days</td>” ;
core_string += “<td>Open</td><td align = ‘left’><small>&nbsp HH &nbsp&nbsp MM<br><input name=’open_hours_a’ type=’text’ size=’1′>:<input name=’open_minutes_a’ type=’text’ size=’1′><input name=’open_ap_a’ type=’radio’ value=’AM’>AM<input name=’open_ap_a’ type=’radio’ value=’PM’>PM</small></td></tr> ” ;
core_string += “<tr rel=event_free_ticketed_multi_day_same_oc>”;
core_string += “<td>Close</td><td align = ‘left’><small>&nbsp HH &nbsp&nbsp MM<br><input name=’close_hours_a’ type=’text’ size=’1′>:<input name=’close_minutes_a’ type=’text’ size=’1′><input name=’close_ap_a’ type=’radio’ value=’AM’>AM<input name=’close_ap_a’ type=’radio’ value=’PM’>PM</small></td></tr>”;

//function to create another field
function create_another_field(output){

//increase the number of field repititions by one
repititions_of_fields = repititions_of_fields + 1;

//produce the html
output.innerHTML = “”;
var str = ”;

//start the table
str = ‘<table border=”4″ style=”border-collapse:collapse;”>n’;
//repeat the fields
for (var x=0; x<repititions_of_fields; ++x){str += core_string;}
//end the table
str += “</table>n”;

//output the string to html
output.innerHTML = str;
}

function remove_a_field(output){
//decrease the number of fields if repititions > 0
if(repititions_of_fields>0){repititions_of_fields = repititions_of_fields – 1;}

//produce the html
output.innerHTML = “”;
var str = ”;

//start the table
str = ‘<table border=”4″ style=”border-collapse:collapse;”>n’;
//repeat the fields
for (var x=0; x<repititions_of_fields; ++x){str += core_string;}
//end the table
str += “</table>n”;

//output the string to html
output.innerHTML = str;
}

window.onload = function(){
var f = document.forms[‘ScheduleForm’], ele = f.elements;

ele[‘sessions_no’].onclick = function()
{return remove_a_field(document.getElementById(‘ScheduleArea’));}

ele[‘sessions_yes’].onclick = function()
{return create_another_field(document.getElementById(‘ScheduleArea’));}
}

</script>
</head>

<body>
<noscript><p>Sorry, this page requires JavaScript to be available and enabled.</p></noscript>
<div style=”text-align:center”>
<form name=”ScheduleForm” action=”test_rego_generate_event_schedule.php” method=”post”>
Would you like to add another group of sessions? <input type=”button” name=”sessions_yes” value=”Yes”><br><br>
Would you like to remove a group of sessions? <input type=”button” name=”sessions_no” value=”Yes”><br><br>
<div id=”ScheduleArea”>&nbsp; &nbsp;</div>
</form>
</div>
</body>
</html>
[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@MrNobodyJan 25.2009 — Well, in the first place, ideally, you should not be rebuilding/replacing the entire table every time you want to add a row. But if you must, then you're also going to have to save field data every time the user enters some. To do that, you'll need to have an [B]onchange[/B] event for each form field to be saved -- and, for free, I'm not going to write all the code you need. Thus, you're going to need to learn JavaScript as you go. If you run into any problems or have a more specific question to ask, I'll help all I can.
Copy linkTweet thisAlerts:
@riddellchrisauthorJan 25.2009 — MrNobody!

Thanks so much! That's perfect, just had no idea where to start but now i do, and i'm loving learning all this stuff so it's probably best for me anyway.

Thanks for all your help (there's been a few times now)
×

Success!

Help @riddellchris spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...