Add new text box based of value entered

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
faizpayi
Forum Newbie
Posts: 14
Joined: Tue Oct 05, 2010 1:24 am

Add new text box based of value entered

Post by faizpayi »

Hi,

Is it possible to add text box based on another value entered in a text box.For example,if a person adds the number of children in a box as 3 ,three new text box should come asking for the name of the children.I tried using if condition in php but when I use <input type="text" name="name1" id="name1" /> inside php tag,error is thrown.

Thanks for the help in advance


Faiz
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Add new text box based of value entered

Post by social_experiment »

Something like :

Code: Select all

<?php
 $value = $_POST['value'];
 if ($value > 0) {
  for ($i = 1; $i <= $value; $i++) {
   echo '<input type="text" name="field_'. $i .'" />';
   echo '<br />';
  }
} ?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
faizpayi
Forum Newbie
Posts: 14
Joined: Tue Oct 05, 2010 1:24 am

Re: Add new text box based of value entered

Post by faizpayi »

Hi...thanks for the codes...this will work on submit or posting the values right...it there anyway that when i choose a value from a list box,suppose 2,then immediately two text box should appear??
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Add new text box based of value entered

Post by social_experiment »

faizpayi wrote:it there anyway that when i choose a value from a list box,suppose 2,then immediately two text box should appear??
You can modify the script to work with a dropdown menu option but this will still have to be submitted if you are going to use php. For an 'immediate' option, you should look into javascript.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply