Multiple Text Boxes help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dracenfels
    New Member
    • Aug 2007
    • 12

    Multiple Text Boxes help

    I need to write a form that asks the usser to define the number of problems (this is simply from a dropdown menu), depending on the number they select they will be asked for a definition of each problem in a seperate text box (one text box for each problem). So if they pick 2 as the number of problem they will be given two text boxes, 5 boxes for selecting 5 from the drop down list.
    I think I need some javascript (for form validation), some php for the form-mailer, but it's the dynamic text boxes I'm at a total loss at.
    Can anyone point me in anything like the right direction?
  • jx2
    New Member
    • Feb 2007
    • 228

    #2
    Originally posted by Dracenfels
    but it's the dynamic text boxes I'm at a total loss at.
    i recomend javascript and DHTML for that purpose
    you should have something like that in your HTML - <div id='textboxes'> </div>

    drop down list with a numbers can call a function i wrote for you below
    Code:
    function boxes(NumberOfBoxes){
         for(i=0;i<NumberOfBoxes;i++){
                  boxes += "<textarea name='textbox[NumberOfBoxes'></textarea>";
         }
                  document.getElementById('textboxes').innerHTML = boxes;
    }
    i haven test it so ...

    i hope that help

    jx2

    Comment

    • Dracenfels
      New Member
      • Aug 2007
      • 12

      #3
      I tried this;

      <html>
      <head>


      </head>


      <body>

      <select name="boxes" onChange="boxes ()">
      <option value="">choose </option>
      <option value="1">1</option>
      <option value="2">2</option>
      </select>
      <script type="text/javascript">
      function boxes(NumberOfB oxes){
      for(i=0;i<Numbe rOfBoxes;i++){
      boxes += "<textarea name='textbox[NumberOfBoxes]'></textarea>";
      }
      document.getEle mentById('textb oxes').innerHTM L = boxes;
      }
      </script>
      <div id='textboxes'>
      boxes should go here
      </div>
      </body>

      </html>
      and it give only one text box with loads of script showing :-(

      Think I'll have another go at work tomorrow.

      Comment

      • jx2
        New Member
        • Feb 2007
        • 228

        #4
        <select name="boxes" onChange="boxes ()">

        // boxes need one parameter "number" of textboxes
        // u need to pass option value to it, e.g. boxes(2);
        // u might try this boxes(this.valu e); - i'm not sure if that will work
        // but u need to pass a value somehow

        regards
        jx2

        Comment

        • jx2
          New Member
          • Feb 2007
          • 228

          #5
          i think you will like me:-)

          [html]<html>
          <head>


          </head>


          <body>

          <select name="boxes" onChange="boxes (this.value)">
          <option value="">choose </option>
          <option value="1">1</option>
          <option value="2">2</option>
          </select>
          <script type="text/javascript"><!--
          function boxes(NumberOfB oxes){
          var i,boxes='';


          for(i=0; i<NumberOfBoxes ; i++ ){
          boxes += "<textarea name='textbox["+i+"]'></textarea>";
          }
          document.getEle mentById('textb oxes').innerHTM L = boxes;
          }
          --></script>
          <div id='textboxes'>
          boxes should go here
          </div>
          </body>

          </html>[/html]

          i check it!! it work very well
          you'll get array textbox[]
          preety cool hah

          regards
          jx2

          Comment

          • Dracenfels
            New Member
            • Aug 2007
            • 12

            #6
            No good, well this line;

            <select name="boxes" onChange="boxes (this.value)">

            gave internet explorer an error (Object doesn't support this property or method), firefox just didn't do anything.

            Any other ideas?

            Comment

            • jx2
              New Member
              • Feb 2007
              • 228

              #7
              [html]<html>
              <head>

              <script type="text/javascript"><!--
              function fboxes(NumberOf Boxes){
              var i,boxes='';


              for(i=0; i<NumberOfBoxes ; i++ ){
              boxes += "<textarea name='textbox["+i+"]'></textarea>";
              }
              document.getEle mentById('textb oxes').innerHTM L = boxes;
              }
              --></script>
              </head>


              <body>

              <select name="boxes" onChange="fboxe s(this.value)">
              <option value="">choose </option>
              <option value="1">1</option>
              <option value="2">2</option>
              </select>


              <div id='textboxes'>
              boxes should go here
              </div>
              </body>

              </html>[/html]

              well - i test it in IE and firefox it works very well in both of them check the syntacs of your code
              i have also change the name of the function that could cos problems...
              it work in my browsers
              IT HAS TO WORK in yours!!!

              regards
              jx2

              Comment

              • jx2
                New Member
                • Feb 2007
                • 228

                #8
                i know what might be wrong if you copy this code from here - lines 10 and 11 should be in the same line my guess is that that might had cos your problems

                regards
                jx2

                Comment

                • Dracenfels
                  New Member
                  • Aug 2007
                  • 12

                  #9
                  Fantastic! got it working, thank you very much (that saved me much hair pulling and headaches).
                  A further question would be how to reference the text boxes in the form-mailer php script. If I define each box

                  $textbox = ($_POST["textbox"]);
                  $textbox1 = ($_POST["textbox1"]);
                  $textbox2 = ($_POST["textbox2"]);

                  I get a result "Array,,,"

                  If I use $textbox["+i+"] = ($_POST["textbox["+i+"]"]);

                  I get nothing.

                  Any ideas on this part?

                  Comment

                  • jx2
                    New Member
                    • Feb 2007
                    • 228

                    #10
                    Originally posted by Dracenfels
                    A further question would be how to reference the text boxes in the form-mailer php script. If I define each box

                    $textbox = ($_POST["textbox"]);
                    $textbox1 = ($_POST["textbox1"]);
                    $textbox2 = ($_POST["textbox2"]);

                    I get a result "Array,,,"

                    Any ideas on this part?
                    well actualy , i did allready answer your question :-)
                    fboxes function creates textareas as an array - look at names !!! of each box $textbox[1] ,$textbox[2] and so on well its no good practise (u should use $_POST['textbox'][1]) but it simplier to explain and to use
                    by the way if you need to know what is in the memory print it!!!
                    e.g.
                    [php]<?php
                    function echovariables($ variable){
                    echo "<ul>";
                    foreach($variab le as $k=>$v){
                    echo "<li>$k =>$v </li>";
                    if(is_array($v) ){
                    echovariables($ v);
                    }
                    }
                    echo "</ul>";
                    }
                    echovariables($ _POST);//can be any variable
                    ?>
                    [/PHP]

                    if you try it u ll see what you got

                    regards jx2

                    Comment

                    • Dracenfels
                      New Member
                      • Aug 2007
                      • 12

                      #11
                      You have been a great help, thank you. I need to brush up on php and arrays.

                      Comment

                      Working...