1

Ok, I have index.html with a form as follows:

<form action="process.php" method="post">
  <table>
    <tr>
      <td><input name="Field[]" type="checkbox" value="Accounting" />Accounting</td>
      <td><input name="Field[]" type="checkbox" value="Finance" />Finance</td>
      <td><input name="Field[]" type="checkbox" value="Marketing" />Marketing</td>
    </tr>
  </table>
</form>

And I have process.php as follows:

<table>
  <tr>
    <th>Field(s):</th>
    <td>
      <?php
        if(isset($_POST['Field']))
        {
          for($i = 0; $i < count($_POST['Field']); $i++)
          { echo $_POST['Field'][$i] . ' '; }
        }
      ?>
    </td>
  </tr>
</table>

Yet for some reason, I only get the first letter of the last checkbox that was checked printed out. Help please!

3
  • That script works as is on my server, for some quick triage, try var_dump($_POST); and verify that the indexes of $_POST['Field'] are what you think they are. Also what server platform/version are you using? Commented Apr 15, 2012 at 4:34
  • I'm running XAMPP on Windows 7 Professional, so Apache/Windows 7 Pro. Also, would I echo var_dump($_POST)? I am still learning PHP, so I don't know. Commented Apr 15, 2012 at 5:39
  • Given that Muhammed's answer below works I'd say that your server environment generated different indexes. var_dump($_POST) is a good debugging tool/statement when things aren't working as expected and it does not need an echo Commented Apr 15, 2012 at 13:09

1 Answer 1

5
Try this one in process.php to get the values from $_POST['Field']

   <table>
    <tr>
     <th>Field(s):</th>
     <td>
      <?php
        if(isset($_POST['Field']))
        {
          foreach ($_POST['Field'] as $value) {
            echo $value;
          }
        }
      ?>
     </td>
    </tr>
   </table>
Sign up to request clarification or add additional context in comments.

3 Comments

It works as you have provided now, however, now my javascript error checking on index.html is no longer validating the form data prior to submission. I did not modify the index.html page, so I am completely confused at this point.
It will not affect the javascript code, since process.php runs at the server side.Please double check the javascript code in index.html or post your index.html code here. So someone can check it and provide you with a crystal clear explanation.
Nevermind, I've tracked down the error using firebug. When I had to rename the checkboxes using the [] appended to the end of their name, it broke the javascript error checking for that field. So instead of validating using the field name, I had to change to the elements array, i.e.: instead of for(var i=0; i<document.forms[0].Field.length; i++) { if(document.forms[0].Field[i].checked == true) break; I had to use for(var i=10; i<=34; i++) { if(document.forms[0].elements[i].checked == true) break; Thanks for all of your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.