Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

php dynamic input forms


ghost's Avatar
0 0

Sorry guys. This may be a stupid question but Im making a form which needs to be dynamic and when the user selects a number (ie. 10) it will produce 10 more input fields.

I need to do this in PHP preferably. What is the best way of doing this. Can someone please post a tut or a page where i can learn from….?

Thanks.


ghost's Avatar
0 0

Yeah thanks. Ive looked there. It doesnt explain how to make things dynamic. Or if it does I couldnt find it.


clone4's Avatar
Perl-6 Wisdom Seeker
0 0

I won't bother writing the whole code, but how about switch statement, where for each value, you echo appropriate input fields: switch ($input_field){ case 1; echo "<form>….</form>"; break; …. }


ghost's Avatar
0 0

ill give it a go. I think it should work.

Much appriciated.


ghost's Avatar
0 0

grr i cant get anything to work. Any more help on the code please?


ghost's Avatar
0 0

what do you mean, do you want it to make the numbers appear on screen without posting? IF so look at ajax.


ghost's Avatar
0 0

Basically something liek this: <SELECT> <option>Select one</option> <option>1</option> <option>2</option> ….

Then which ever number they chose I want that many input fields to come up ie. if they chose 2 it will echo "<input type="text" name="whatever">"; twice. or soemthing.


ghost's Avatar
0 0

Yes i understand that , but do you want the page to be posted or not?


ghost's Avatar
0 0

preferably not.


ghost's Avatar
0 0

Ok, then look at onChange events and ajax


ghost's Avatar
0 0
&lt;body&gt;
&lt;form action=&quot;&lt;?=$_SERVER[&#39;PHP_SELF&#39;]?&gt;&quot; method=&quot;POST&quot;&gt;
&lt;?php
 if (isset($_POST[&#39;inputnumber&#39;))
 {
   $count = htmlentities($_POST[&#39;inputnumber&#39;]);
   if (is_numeric($count))
   {
     for ($x = 0; $x &lt; $count; $x++)
     {
       echo &#39;Input #&#39;.$x.&#39;:  &lt;input type=&quot;text&quot; name=&quot;input&#39;.$x.&#39;&quot; /&gt;&lt;br /&gt;&#39;;
     }
   }
  }
  else
  {
    echo &#39;&lt;input type=&quot;text&quot; name=&quot;inputnumber&quot; /&gt;&lt;br /&gt;&#39;;
  }
?&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
&lt;/form&gt;
&lt;/body&gt;```

Of course, you&#39;ll want to do more to protect your inputs than just &quot;htmlentities()&quot;... I just put that there as a placeholder.  Also, you&#39;ll need to handle your own form submissions for after they choose how many inputs.  Just had to write some code for it since no one seemed to hit on the most efficient way.  Good luck.

You... may find it a bit troublesome to update a form with additional fields using JS if you intend to POST it normally with PHP.  Also, if you don&#39;t know PHP that well (or at all), there&#39;s no reason for you to try AJAX just yet.  My advice would be to just stick with PHP for now and don&#39;t sweat the postback.

ghost's Avatar
0 0

Thanks man. I'm glad you could help me.

Thanks a lot =]]


ghost's Avatar
0 0

No problem… couldn't stand to see so many wrong answers in the thread. There's still a lot more work to be done with making it actually DO something. Just wanted to illustrate the concept.


crashbird's Avatar
-=CodeGuru=-
0 0

hey.. wouldn't that code.. require him to post the page.. I guess if you want it to be dynamic.. learn a little of ajax.. that should solve your problem..

And i am too lazy to code that for you.. I hope i helped…


ghost's Avatar
0 0

crashbird wrote: hey.. wouldn't that code.. require him to post the page.. I guess if you want it to be dynamic.. learn a little of ajax.. that should solve your problem..

Zephyr_Pure wrote: You… may find it a bit troublesome to update a form with additional fields using JS if you intend to POST it normally with PHP. Also, if you don't know PHP that well (or at all), there's no reason for you to try AJAX just yet. My advice would be to just stick with PHP for now and don't sweat the postback.

Read my post next time before you make obvious statements like that.

And i am too lazy to code that for you.. I hope i helped…

Can't imagine he was asking for you to code it for him… he was asking for advice. Only reason I put code in this thread is because the people before me had the concept completely wrong.

We can tell you're lazy… it was reflected in your post. So, no, you didn't help… at all. Assface.