After reading and trying multiple ways to read a checkbox with multiple values

took a year off as a newbie and wanted to try again. I am simply trying to read multiple checkbox entries. It looks like the values are not loading into the global POST or I am not properly accessing them as I only get the last checkbox value when I can get the code to work. mostly I get the error: Warning : foreach() argument must be of type array|object, string given in /home/funkandg/!sites/mike/combolisk.org/www/mikedocs/pics/annaappend.php on line 7

My simple form:

<html>
<head>
  <style>
  th, td {
   border: 1px solid black;
  }
  </style>
</head>
  <body>
  <div>
    <h1>Select multiple</h1>
    <p>Please select the checkboxes for articles you want displayed. Continue to the text and use the audio button to listen.</p>

    <form action = "annaappend.php" method = "post">
      <button type = "submit" name = "submit">Load</button>
      <table style="width:100%">
      <tr>
            <th>Check</th>
            <th>#</th>
            <th>Approximate Date</th>
            <th>Title</th>
      </tr>
 <tr>
            <td><input type = "checkbox" name = "check" value = "file 1">file 1</td>
            <td>Article #1</td>
            <td>1/1/2014</td>
            <td>My Dear Archbishop George</td>
      </tr>
      <tr>
            <td><input type = "checkbox" name = "check" value = "file 2">file 2</td>
            <td>Article #2</td>
            <td>6/5/14</td>
            <td>The Nut is Cracked</td>
      </tr>
      <tr>
            <td><input type = "checkbox" name = "check" value = "file 3">file 3</td>
            <td>Article #3</td>
            <td>6/5/14</td>
            <td>For a Deeper Understanding</td> 

      </tr>

</table>
<br><br><br><br><br><br>
    <input type = "text" name="hidden" placeholder = "not used">


  </form>

  </div>
</body>
</html>

The php copied from an example changing just the variable$

<?php
//retrieve playlist from checkboxex
if (isset($_POST['check']))
{
if(!empty($_POST['check']))
  {
  foreach($_POST['check'] as $fname)
    {
      echo $fname. "<br>";
      echo "end of file";
    }
  }
}
echo "Thank you. Use the button above the text to listen";

 ?>

Your help is greatly appreciated.

so I found I needed [] after my name to load multiple checkbox values. I change to name = ‘check[]’ but now the array appears empty with no errors

Don’t take this the wrong way, but looking at your HTML and Code, I would would concentrate on learning HTML/CSS better before tackling PHP coding.

  1. Never use table elements inside a form - a small example
<form method="post" action="process.php">
  <input type="checkbox" name="newsletter" value="1">Subscribe to Newsletter<br>
  <input type="submit" name="submit" value="Submit">
</form>

and the PHP which normally goes above the HTML.

if($_SERVER['REQUEST_METHOD'] === 'POST') {
  if(isset($_POST['newsletter']) && $_POST['newsletter'] == 1) {
    // Checkbox is checked, do something
    echo "You are subscribed to our newsletter!";
  } else {
    // Checkbox is not checked, do something else
    echo "You are not subscribed to our newsletter.";
  }
}

You can make nice forms without tables - https://www.fanoflego.com/register.php

the action isn’t needed if it is going to the same page (File).

I am posting to a different page. Aside from having to learn all the languages, my basic question is how to load multiple checkbox values that I can pass to the different page. I am not getting an echo inside my check for empty so nothing is getting stored in the html array for multiple checkboxes which is the subject of the post. My form once I review how to make pretty will have over 4k entries. So I need a loop and array to pass only the items selected. And I don’t want to write 4000 lines of PHP to deal with each name as your example implies.

<html>
<head>
</head>
  <body>
  <div>
    <h1>Select multiple</h1>
    <p>Please select the checkboxes for articles you want displayed. Continue to the text and use the audio button to listen.</p>

    <form action = "annaappend.php" method = "post">
      <button type = "submit" name = "submit">Load</button><br>

            <input type = "checkbox" name = "check[]" value = "file 1">file 1<br>
            <input type = "checkbox" name = "check[]" value = "file 2">file 2<br>
            <input type = "checkbox" name = "check[]" value = "file 3">file 3<br>
            <input type = "checkbox" name = "check[]" value = "file 4">file 4<br>

<br>
    <input type = "text" name="hidden" placeholder = "not used">


  </form>

  </div>
</body>
</html>

The last posted form code works for me with the previously posted form processing code.

Most likely you are opening the form page directly through the file system in your browser, rather than through a url on a web server, which means the form’s action page is also being opened directly through the file system in your browser and the raw php code is just being output to the browser (a ‘view source’ of the page will show the php code.)

Some other possibilities include not reloaded the form page after you made changes to it or copy/pasting code from somewhere on the internet where it was ‘published’ and there are characters present in it that are not straight ascii characters and don’t have any meaning to the browser or to php, but which might be converted back to good characters when posting in a help forum and therefore work when tried.

Your form and form processing should be on the same page. This will simple all the code, make it easier to secure the code, and provide a better user experience (you can re-display the form if there are user/validation errors and repopulate the field values/selected-checkboxes so that the user doesn’t need to keep reentering or reselecting choices.) The code for any page should be laid out in this general order - 1) initialization, 2) post method form processing, 3) get method business logic, and 4) html document.

I created my pages using atom. I shouldn’t have any extra codes but may recreate them from scratch as I am still passing through the php and looks like my form array is empty.

I originally was testing on a live remote server. I decided to try on localhost to see if there was a php configuration issue. It did not work. After reading your post, I added the local host server to the form method a d can see from the cmd window of the server that it is processing. It does say closed without sending a request; it was probably just an unused speculative preconnection.

Thank you for verifying it should work and for the design tips. The form is mostly for my own research to sift through and make a Playlist from 4000 url titles. I want to sent the output to another page so I can start a txt audio button and then listen to multiple selections during windshield time.

I have a great respect for you guys that do this for a living and help those build their skills. I think it would drive me insane.

So I finally found the issue after tearing the pieces down and putting echo statements in to see where I got to in the code. The brackets [ ] are necessary to load the values of the checkbox IDs from the form on the HTML input side. The brackets are not part of the name even though within the double quotation marks. I added them to my php and therefore I was trying to read a global variable which did not exist. Once I removed the brackets, the PHP performed correctly. And I got some good inputs from the community along the way. Thanks to all.

I finally got an iframe to display my pdf file. I couldn’t get it to come up on my PC browser using edge. I was able to get it to display on Android but it only co.es up with an open button and then loads the pdf as a new window. I was hoping to make a Playlist form that would allow multiple pdf files to be daisy chained together to print, read or have converted to speech. Is there a better method to do this? The other tags appear outdated from what I have found.

Sponsor our Newsletter | Privacy Policy | Terms of Service