0

Here is the file (test.php) than I try to run on my localhost. Javascript works and it creates the text boxes. PHP-part does not work and makes problem.

    <html>
    <head>
    <title>Test</title>

     <script>

     function newCheckbox() {
    var aLabel = document.form1.getElementsByTagName('label');
    var last = aLabel[aLabel.length-1];
    var label = document.createElement('label');
    label.appendChild(Box(aLabel.length));

    label.appendChild(document.createTextNode('     '+document.getElemenById('text').value));       
    last.parentNode.insertBefore(label, last);
     document.getElementById('text').value = '';
     }

function Box(num) {
var elm = null;
 try {
     elm=document.createElement('<input type="checkbox" class="chk">');
    }
  catch(e) {
     elm = document.createElement('input');
    elm.setAttribute('type', 'checkbox');
    elm.className='chk';
   }
    return elm;
    }

    function delBoxes(){ 
    var texts = document.form1.getElementsByTagName('label');
    var chbox = document.form1.getElementsByClassName('chk');
    for(var i = 0; i<texts.length-1; i++){
    if(chbox[i].checked){
       chbox[i].parentNode.removeChild(chbox[i]);
       texts[i].parentNode.removeChild(texts[i]);
        }
     }
  }
  </script>

   <?php

   $text_0= $_POST['text[0]'];
   echo $text_0;

 $textboxes = array();    
 for($i = 0;$i<count($_POST['text[]']); $i++)

   {

      $textboxes = $_POST['text'][$i];

    }


       $data=$textboxes

    ?>
  </head>
  <body>

    <form action="test.php" name="form1" method="post">
     <div>
      <label>Checkbox text:<input type="text" name="text[]"></label><br>
    <input type="button" onclick="newCheckbox();"value="add">
    <input type="button" value="Delete" onclick = "delBoxes();" />

    </div>
    </form>

    </body>
   </html>

When I run the code I have the following problem Undefined index: text[0] Undefined index: text[]

I appreciates your help.

learning
  • 1
  • 2
  • Not sure that I fully understand what you are doing, but I think you just need to add bracketed names to your inputs. See:[How to get form input array into PHP array](http://stackoverflow.com/questions/3314567/how-to-get-form-input-array-into-php-array) – Yogi May 31 '15 at 19:28
  • http://www.html-form-guide.com/php-form/php-form-checkbox.html – Zast May 31 '15 at 22:58

0 Answers0