1 Replies - 384 Views - Last Post: 12 May 2013 - 03:22 PM

#1 blitzfx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 16-August 09

How to make dynamically appearing forms

Posted 09 May 2013 - 10:39 PM

I'm not sure what language to write this up in, so I was hoping to get some direction (and maybe some examples) on where to start looking.

What I'm thinking of making is having a drop down list with something like "How many cars do you have", followed by the option to select 1,2,3,4,5 (maximum 5)
If the user selects 1, then a form would appear below like
<form name="new" method = "post" action = "submit.php">
<u><b>Car1 details:</b></u><br>
<textarea rows="5" cols="20" name="car1" wrap="physical"></textarea><br>
<input type = "submit" name="submit" value = "submit"/>
</form>



then if the user selects 2, then it would look like
<form name="new" method = "post" action = "submit.php">
<u><b>Car1 details:</b></u><br>
<textarea rows="5" cols="20" name="car1" wrap="physical"></textarea><br>
<u><b>Car2 details:</b></u><br>
<textarea rows="5" cols="20" name="car2" wrap="physical"></textarea><br>
<input type = "submit" name="submit" value = "submit"/>
</form>

and so on.

I think a common example would be in online checkout/payments where on the shipping page there are two menus "Country" and "State" and by default, the "State" drop down menu lists the states in North America but when you select a different Country, the "State" menu changes immediately to reflect the Country.

Sorry if this has been answered already. I didn't know what to search for.

Is This A Good Question/Topic? 0
  • +

Replies To: How to make dynamically appearing forms

#2 xxxjj18  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 53
  • View blog
  • Posts: 167
  • Joined: 30-November 11

Re: How to make dynamically appearing forms

Posted 12 May 2013 - 03:22 PM

You could use jQuery to add the form to the page. Like when the user changes the dropbox value and presses a button to generate the form, then create a string that will be used for the form's html and set a div's content to that string. Something like this:

$('#create_form').click(function() {
	
	var dropBoxValue = parseInt($('#dropbox_select').val());
	
	var form_html = "<form action='submit.php' method='post'>";
	
	for(var i = 1; i < dropBoxValue; ++i) {
		
		form_html += "<u><b>Car"+i+" details:</b></u><br>";
		form_html += "<textarea rows='5' cols='20' name='car"+i+" wrap='physical'></textarea><br>";
	}
	
	form_html += "<input type='submit' name='submit id='submit' value='Submit' />";
	form_html += "</form>";
	
	$('#some_div').html(form_html);
	
});



Of course, you would have to include the jQuery library in your page as well.

This post has been edited by xxxjj18: 12 May 2013 - 03:25 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1