I am new to php and wanted to now, how can I add text fields at runtime. I have created a php page with a simple dropdown list in it.The dropdown list has numbers 1,2,3 and so on. What I want to do is that whenever a user selects a number from dropdown list, the same number of text fields (along with labels) appear. Example 1 textfield for 1 (with label), 2 textfields for 2 and so on . If someone can give me any code example or a link to understand, It will be very helpful . Thanks alot :)
3
-
3sounds like you want javascript not php. to do it with php the form would have to be submitted to create the textfieldsuser557846– user5578462012-07-12 04:25:03 +00:00Commented Jul 12, 2012 at 4:25
-
@Dagon can you please give me any code example or a link to do it with javascript? thanksuser841852– user8418522012-07-12 04:26:36 +00:00Commented Jul 12, 2012 at 4:26
-
I'm fond of the jquery Library jquery.comuser557846– user5578462012-07-12 04:29:02 +00:00Commented Jul 12, 2012 at 4:29
Add a comment
|
3 Answers
I would use jquery, a JavaScript library as Dagon suggested. First your html:
<form>
<input class="target" type="text" value="Field 1" />
<select class="target">
<option value="1" selected="selected">Option 1</option>
<option value="2">Option 2</option>
</select>
</form>
<div id="other">
</div>
Then your javascript (using jQuery):
$('.target').change(function() {
$("select option:selected").each(function () {
$('#other').append("<label>" + $(this).text() + "</label><input type='text' value='' />");
});
});
For your reference:
Comments
You can use jQuery to help you. Use change() function ( http://api.jquery.com/change/ ) to get the drop down list value and create another jQuery function to populate the text fields.
This tutorial might help you http://charlie.griefer.com/blog/2009/09/17/jquery-dynamically-adding-form-elements/