prudens Posted May 19, 2008 Share Posted May 19, 2008 Hey, How do I use HTML/DHTML to create textboxes based on what user selects? For example, I want to have a dropdown boxes with selections of "1/2/3/4/5/6/..." and based on that number, the website creates that many textboxes, each with a unique name. Because eventually I want to record the text in those textboxes into an SQL table. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/106264-dynamically-create-textboxeslistboxes/ Share on other sites More sharing options...
xnowandtheworldx Posted May 19, 2008 Share Posted May 19, 2008 Well, you could use ajax to do it without refreshing the page or do it like this in php //if(!isset($_POST['submit'])){ uncomment this if you don't want this to show up if it's been submitted. Uncomment the other too. <form method="POST" action="<?= echo $_SERVER['PHP_SELF'] ?>"> <select name="textboxes"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select> <input type="submit" value="Create!" name="submit"> </form> //} <?php if(isset($_POST['submit'])) { echo "<form method=\"POST\" action=\"page.php\">"; //create the new form for these textboxes for($i; $i <= 9; $i++){ //we only go to nine since the for loop is 0 based you can change it to, for($i = 1; $i <= 10; $i++){ echo "<input type=\"text\" name="{$i}">"; } echo "<input type=\"submit\" name=\"submit\" value=\"Submit!\">"; } ?> Hope this helps you a bit? Anymore questions feel free to ask! EDIT: There seems to be a bug in my code. Trying to fix it now! Sorry. Quote Link to comment https://forums.phpfreaks.com/topic/106264-dynamically-create-textboxeslistboxes/#findComment-544629 Share on other sites More sharing options...
prudens Posted May 19, 2008 Author Share Posted May 19, 2008 What is Ajax? Can I use Ajax on Facebook? Quote Link to comment https://forums.phpfreaks.com/topic/106264-dynamically-create-textboxeslistboxes/#findComment-544632 Share on other sites More sharing options...
prudens Posted May 19, 2008 Author Share Posted May 19, 2008 Parse error: syntax error, unexpected T_ECHO in Untitled-4.php on line 2 Quote Link to comment https://forums.phpfreaks.com/topic/106264-dynamically-create-textboxeslistboxes/#findComment-544633 Share on other sites More sharing options...
xnowandtheworldx Posted May 19, 2008 Share Posted May 19, 2008 Sorry about that mate, trying to fix it now. Quote Link to comment https://forums.phpfreaks.com/topic/106264-dynamically-create-textboxeslistboxes/#findComment-544636 Share on other sites More sharing options...
xnowandtheworldx Posted May 19, 2008 Share Posted May 19, 2008 Fixed code..no wonder I wasn't able to fix it I was saving a different copy of it to another area every time! lol Here's the fixed code... <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <select name="textboxes"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select> <input type="submit" value="Create!" name="submit"> </form> <?php if(isset($_POST['submit'])) { echo "<form method=\"POST\" action=\"page.php\">"; //create the new form for these textboxes for($i; $i <= 9; $i++){ //we only go to nine since the for loop is 0 based you can change it to, for($i = 1; $i <= 10; $i++){ echo "<input type=\"text\" name=\"{$i}\"><br/>"; } echo "<input type=\"submit\" name=\"submit\" value=\"Submit!\">"; echo "</form>"; } ?> And also, I don't believe you can use ajax with facebook, but then again, i've never tried. Quote Link to comment https://forums.phpfreaks.com/topic/106264-dynamically-create-textboxeslistboxes/#findComment-544637 Share on other sites More sharing options...
prudens Posted May 20, 2008 Author Share Posted May 20, 2008 Is there anyway to create it dynamically? Instead of reloading a new page with it, can we just instantly make textboxes??? Also your code doesn't work... it creates 9 textboxes no matter what the Dropdown box value was.... Quote Link to comment https://forums.phpfreaks.com/topic/106264-dynamically-create-textboxeslistboxes/#findComment-545403 Share on other sites More sharing options...
wrongmove18 Posted May 20, 2008 Share Posted May 20, 2008 Is there anyway to create it dynamically? Instead of reloading a new page with it, can we just instantly make textboxes??? Also your code doesn't work... it creates 9 textboxes no matter what the Dropdown box value was.... This might help.. <script type="text/javascript"> function create_textbox(container, count){ var cont = document.getElementById(container); // reset container contents cont.innerHTML = ""; for(var i=0; i<count; i++){ var input = document.createElement("input"); input.type = "text"; input.name = "name"+i; cont.appendChild(input); } } </script> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <select name="textboxes" onchange="create_textbox('input_container', this.value)"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select> <div id="input_container"></div> </form> Quote Link to comment https://forums.phpfreaks.com/topic/106264-dynamically-create-textboxeslistboxes/#findComment-545476 Share on other sites More sharing options...
haku Posted May 20, 2008 Share Posted May 20, 2008 And also, I don't believe you can use ajax with facebook, but then again, i've never tried. You don't use ajax with sites, the site designers will either program ajax functions into the site or not. When you use the site, you will use the ajax if they have it, or not if they don't. Facebook has LOTS of ajax in it. Anytime you click something and the page changes without reloading, that is an ajax function. Quote Link to comment https://forums.phpfreaks.com/topic/106264-dynamically-create-textboxeslistboxes/#findComment-545504 Share on other sites More sharing options...
prudens Posted May 20, 2008 Author Share Posted May 20, 2008 wrongmove18: your code doesn't seem to work... is it IE only? ??? Quote Link to comment https://forums.phpfreaks.com/topic/106264-dynamically-create-textboxeslistboxes/#findComment-545825 Share on other sites More sharing options...
wrongmove18 Posted May 22, 2008 Share Posted May 22, 2008 it works for me.. IE and FF Quote Link to comment https://forums.phpfreaks.com/topic/106264-dynamically-create-textboxeslistboxes/#findComment-547184 Share on other sites More sharing options...
prudens Posted May 23, 2008 Author Share Posted May 23, 2008 Where can I find more information on AJAX, especially INPUTS/DROPBOX? Quote Link to comment https://forums.phpfreaks.com/topic/106264-dynamically-create-textboxeslistboxes/#findComment-547848 Share on other sites More sharing options...
haku Posted May 23, 2008 Share Posted May 23, 2008 Google. Quote Link to comment https://forums.phpfreaks.com/topic/106264-dynamically-create-textboxeslistboxes/#findComment-547870 Share on other sites More sharing options...
prudens Posted May 25, 2008 Author Share Posted May 25, 2008 link pls.? Quote Link to comment https://forums.phpfreaks.com/topic/106264-dynamically-create-textboxeslistboxes/#findComment-549307 Share on other sites More sharing options...
haku Posted May 25, 2008 Share Posted May 25, 2008 Google Quote Link to comment https://forums.phpfreaks.com/topic/106264-dynamically-create-textboxeslistboxes/#findComment-549849 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.