title: Loop through inputs and get checked value
body: <p>I'm trying to get a list of radio inputs from a form that are grouped by their names, and they all have different names.  I'm trying to create an array with them, if they are checked then append a <code>1</code> to the array, otherwise append a <code>0</code>.  None of the alert boxes pop up, so I assume that the function <code>processDataForFinalization</code> doesn't get called.  </p>
  
  <p>I didn't know if something was wrong with how I'm appending to the array.  Or if it's a bigger error than that.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>EDIT4</strong> Here is the updateBallot.php code</p>
  
  <p><code>TwoResults.txt</code> looks like:</p>
  
  <pre><code>0 0 0 0 0 0 0 0 
  3 4 1 
  
  &lt;?php
  
  $name = $_POST['name'];
  $points = $_POST['points']; //array of user vote
  echo $points;
  
  $txt_file = file_get_contents($name.'Results.txt');
  
  $rows = explode("\n", $txt_file); // rows-array separated by new line
  
  foreach($rows as $row =&gt; $data) 
  {
      $row_datas = explode(' ',$data);
  }
  
  $goods = $row_datas[0];
  $goodies = $row_datas[1];
  
  $newArr = [];
  for($j=0;$j&lt;count($points);$j++) {
      $newArr[$j] = $goods[$j] + $points[$j];
  }
  
  $newText = "";
  foreach($newArr as $nArr =&gt; $nData) {
      $newText .= $nData." ";
  }
  $newText .= "\n";
  
  $secondNewArr = [];
  
  $sizeOfBottom = count($goodies);
  
  for($k=0;$k&lt;$sizeOfBottom;$k++) {
      $secondNewArr[$k] = $goodies[$k];
  }
  
  foreach($secondNewArr as $neArr =&gt; $neData) {
      $newText .= $neData." ";
  }
  $myFile = fopen($txt_file,"w+");
  fwrite($myFile, $newText);
  fclose($myFile);
  
  ?&gt;
  </code></pre>
  
  <p><strong>EDIT3</strong> Here is the output HTML from the page.</p>
  
  <pre><code>&lt;form action="submitElection.php" method="post"&gt;
          &lt;input type="hidden" name="id" value="20"&gt;
          &lt;input type="hidden" name="numQs" id="numQs"&gt;
          &lt;input type="hidden" name="arr" value="Array"&gt;
  
          &lt;script&gt;
              var a = document.getElementById("numQs");
              a.value = numQs;
          &lt;/script&gt;
  
          &lt;div id='ballotElement'&gt;&lt;h4&gt;&lt;u&gt;q&lt;/u&gt;&lt;/h4&gt;                  
          &lt;input type="radio" name="input0" value="1"&gt;
                    a                  &lt;br&gt;
                                    &lt;input type="radio" name="input0" value="1"&gt;
                    a                  &lt;br&gt;
                                    &lt;input type="radio" name="input0" value="1"&gt;
                    a                  &lt;br&gt;
                  &lt;/div&gt;                
                  &lt;script&gt;
                    var numQs = 1;
                  &lt;/script&gt;
                  &lt;div id='ballotElement'&gt;&lt;h4&gt;&lt;u&gt;q&lt;/u&gt;&lt;/h4&gt;                        
                  &lt;input type="radio" name="input1" value="1"&gt;
                    a                  &lt;br&gt;
                                    &lt;input type="radio" name="input1" value="1"&gt;
                    a                  &lt;br&gt;
                                    &lt;input type="radio" name="input1" value="1"&gt;
                    a                  &lt;br&gt;
                                    &lt;input type="radio" name="input1" value="1"&gt;
                    a                  &lt;br&gt;
                  &lt;/div&gt;                
                  &lt;script&gt;
                    var numQs = 2;
                  &lt;/script&gt;
                  &lt;div id='ballotElement'&gt;&lt;h4&gt;&lt;u&gt;q&lt;/u&gt;&lt;/h4&gt;                  
                  &lt;input type="radio" name="input2" value="1"&gt;
                    a                  &lt;br&gt;
                  &lt;/div&gt;                
                  &lt;script&gt;
                    var numQs = 3;
                  &lt;/script&gt;
                  &lt;br&gt;&lt;br&gt;&lt;br&gt;      
               &lt;div class="submit"&gt;
               &lt;input type="submit" value="Submit ballot" class="btn btn-primary btn-large" onclick="return confirmSubmit()"&gt;
        &lt;/div&gt;
          &lt;/form&gt;
  </code></pre>
  
  <p><strong>EDIT2</strong> Here is the HTML from the page.</p>
  
  <pre><code>&lt;form action="submitElection.php" method="post"&gt;
          &lt;input type="hidden" name="id" value="&lt;?php echo $id;?&gt;"&gt;
          &lt;input type="hidden" name="numQs" id="numQs"&gt;
          &lt;input type="hidden" name="arr" value="&lt;?php echo $arr;?&gt;"&gt;
  
          &lt;script&gt;
              var a = document.getElementById("numQs");
              a.value = numQs;
          &lt;/script&gt;
  
          &lt;?php
  
          if (file_exists($name.'.txt')) {
            if($type==1) {
              $inputType = "checkbox";
            } else {
              $inputType = "radio";
            }
  
              $txt_file = file_get_contents($name.'.txt');
              $rows = explode("\n", $txt_file);
              array_shift($rows);
  
          $vals = 0;
  
              foreach($rows as $row =&gt; $data) 
              {
                $arrCount = 0; // number of elements in array
                  echo "&lt;div id='ballotElement'&gt;";
                  $row_data = explode('^',$data);
  
                  $info[$row]['questions'] = $row_data[0];
                  $info[$row]['answers'] = $row_data[1];
  
                  echo "&lt;h4&gt;&lt;u&gt;".$info[$row]['questions'] . "&lt;/u&gt;&lt;/h4&gt;";
                  //echo $info[$row]['answers'] . "&lt;br&gt;";
  
                  $lines = explode("~",$info[$row]['answers']);
                  foreach($lines as $line =&gt; $lData) { 
  
                  $answers[$row][$line] = $lData;
                  if($answers[$row][$line] !== "") {
                    $arrCount++;
                  ?&gt;
                    &lt;input type="&lt;?php echo $inputType; ?&gt;" name="input&lt;?php echo $row; ?&gt;" value="1"&gt;
                    &lt;?php echo $answers[$row][$line]; ?&gt;
                    &lt;br&gt;
                  &lt;?php }
                    $vals++;
                  }
                if($writeIn == "y") { 
                $arrCount++; ?&gt;
                  &lt;input type="text" name="writeIn&lt;?php echo $row; ?&gt;" placeholder="Write in"&gt;
              &lt;?php 
                  }
                  echo "&lt;/div&gt;";
                  array_push($arr, $arrCount);
                  ?&gt;
  
                  &lt;script&gt;
                    var numQs = &lt;?php echo count($arr); ?&gt;;
                  &lt;/script&gt;
                  &lt;?php
  
                }
          } else {
            header('Location: error.php');
          }
              echo "&lt;br&gt;&lt;br&gt;&lt;br&gt;";
  
          &lt;input type="submit" value="Submit ballot" class="btn btn-primary btn-large" onclick="return confirmSubmit()"&gt;
          &lt;/form&gt;
  </code></pre>
  
  <p><strong>EDIT</strong> getActualData() is called from getData()
  Here is <code>getData()</code>, this function works, so I'm not sure why the <code>getActualData()</code> doesn't work since they are basically the same thing.</p>
  
  <pre><code>function getData() {
  
  
          var parts = 0;
          for(var i=0; i&lt;numQs; i++){
            var checked = false;
            var inp = document.getElementsByName('input'+i);
            for(var j=0; j&lt;inp.length &amp;&amp; checked == false; j++){
              if(inp[j].checked) {
                checked = true;
                parts++;
              }
            }
          }
          if(parts != numQs){
            window.alert('Please vote for at least one candidate per race.');
            return false;
          } else {
            window.alert('Okay');
            getActualData();
            //return true;
          }
  
      }
    function processDataForFinalization() {
      $.ajax({
        url: 'updateBallot.php',
        type: 'post',
        data: {"points" : val, "name":name},
        success: function(data) {
          // Do something with data that came back. 
          window.alert(data);
          //return true;
  
        }
      });
  
    }
  
    var val = [];
    function getActualData() {
  
      for(var k=0;k&lt;numQs;k++) {
  
        var inpu = document.getElementsByName('input'+k);
        for(var jk=0;jk&lt;inpu.length;jk++) {
  
          window.alert('almost there');
          if(inpu[jk].checked) {
            window.alert('hello');
            val.array_push(1);
            window.alert(val.length);
          } else {
            val.array_push(0);
  
          }
        }
      }
  
     processDataForFinalization();
  
    }
  </code></pre>