php dynamic input forms
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.
<body>
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<?php
if (isset($_POST['inputnumber'))
{
$count = htmlentities($_POST['inputnumber']);
if (is_numeric($count))
{
for ($x = 0; $x < $count; $x++)
{
echo 'Input #'.$x.': <input type="text" name="input'.$x.'" /><br />';
}
}
}
else
{
echo '<input type="text" name="inputnumber" /><br />';
}
?>
<input type="submit" value="Submit" />
</form>
</body>```
Of course, you'll want to do more to protect your inputs than just "htmlentities()"... I just put that there as a placeholder. Also, you'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'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.
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.