vendredi 29 juillet 2016

how to add multiple values from checkbox into .txt file

For a simple voting system, i put the values into a .txt file. This is the array i use:

$quickpolloptions = ['Mozilla', 'Chrome', 'Opera', 'IE', 'Safari'];

This is the form:

<form method="post" id="quickpoll">
   foreach ($quickpolloptions as $key => $value) {
         echo "<tr>";
             echo "<td>";
                    echo "<label>$value</label>";
         echo "</td>";
         echo "<td>";
                    echo "<input type='checkbox' name='checkboxvote[]' value='$key'><br>";
         echo "</td>";
      echo "</tr>";
    }
<input type="submit" value="Submit">
</form>

This is how i store the data:

$result_file = "data/vote_result.txt";

if (file_exists($result_file)) {
   $results = explode(',', file_get_contents('data/vote_result.txt'));
} else {
   // start with zeros if you don't have a file yet
   $results = array_fill(0, count($quickpolloptions), 0);
}

// below i am trying to read each value fromn checkbox and store in .txt file

if (isset($_POST['checkboxvote'])) {

  foreach ($_POST['checkboxvote'] as $checkbox) {
    $results[$_POST['checkboxvote']]++;
    file_put_contents('data/vote_result.txt', implode(',', $results));
  }
}

So i do not succeed in the last part: to put multiple values in the txt file.

How can i do that?




Aucun commentaire:

Enregistrer un commentaire