-2

This is a page where I have a table with some input that must be sent to another page using a form.

<form action="another-page.php" method="POST" id="action-form"></form>
      <table id="filter-table" class="table table-striped">
        <thead>
          <tr>
            <th> CODE </th>
            <th> NAME </th>
            <th> CONF </th>
            <th> SELECT </th>
          </tr>
         </thead>
         <tbody>
          <tr>
               <td>A0294850</td>
               <td>Test</td>
               <td>A792</td>
               <td><input type="checkbox" name="[A0294850]checkbox" value="A0294850" form="action-form"></td>
          </tr>
         </tbody>
       </table>
<div class="row-fluid">
   <div class="span12 module_cont module_promo_text">
      <div class="shortcode_promoblock ">
         <div class="row-fluid">
            <div class="span9 promo_text_block">
               <h4>Submit the form</h4>
            </div>
            <div class="span3 promo_button_block">
               <input type="submit" class="promo_button" value="OK" form="action-form">
            </div>
            </div>
           </div>
           </div><!--.module_cont -->
           <div class="clear"><!-- ClearFIX --></div>
           </div><!-- .row-fluid -->

In the other page ('another-page.php') I try to print the $_POST value but I receive an empty array as the response.

HERE IS THE CODE

<?php
    print_r($_REQUEST); die();
?>

How should I submit the form?

I've tried also to include all the table and html code inside the tag of form but it also doesn't work.

Henders
  • 1,154
  • 1
  • 22
  • 25
giovaZ
  • 1,374
  • 2
  • 16
  • 51

1 Answers1

7

You are starting and ending your form on the first line of your code:

<form action="another-page.php" method="POST" id="action-form"></form>

The closing tag signals the end of that form and thus nothing in your table is actually part of the form. You need to surround the table with the form tags like this:

<form>
   <table></table>
</form>

Check out some examples of form usage.


Update: It transpires you are using the form attribute on your fields so closing the form is not a problem. The problem actually sits with the name of your checkbox. As quoted from the docs:

Note: Only form elements with a name attribute will have their values passed when submitting a form.

This means that when you have an invalid name for your input, it is treated as blank and thus is not passed through. If you change it to something like this:

<input type="checkbox" name="checkbox[A0294850]" value="A0294850" form="action-form">

You will get an array like this from your print script:

Array ( [checkbox] => Array ( [A0294850] => A0294850 ) )

The reason you had an issue with starting the name with a [ is that certain server-side languages will attempt to interpret this as an instruction to create an array. Unfortunately, PHP will not create the array because you haven't named it so it cannot appear in the associative $_POST array as the text that proceeds the bracket needs to be used as the key.

Henders
  • 1,154
  • 1
  • 22
  • 25
  • 2
    @Ali why? I'm quite sure this is the problem, or at least one of them – Andreas Mar 31 '16 at 11:00
  • 1
    @Ali: can u explain your statement? – devpro Mar 31 '16 at 11:03
  • It is in fact THE answer to why OPs form is not posting. – Stuart Mar 31 '16 at 11:06
  • i know that i'm doing it. but in the input tags there are attribute 'form = action-form' refeer to this post http://stackoverflow.com/questions/5967564/form-inside-a-table on the Vladimir answer – giovaZ Mar 31 '16 at 12:19
  • @giovaZ Are you using HTML 5? The comments stipulate you need to be for that to work? – Henders Mar 31 '16 at 12:33
  • @Henders yhea i'm using html5 for the website, also i've tried to wrap all the table inside the form tags but it doesn't work – giovaZ Mar 31 '16 at 12:47
  • @giovaZ It's being caused by your name for the checkbox. You cannot start it with a bracket. I'll update my answer with some more information. – Henders Mar 31 '16 at 12:52