Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/106264-dynamically-create-textboxeslistboxes/
Share on other sites

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.

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.

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>

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.