views:

458

answers:

2

Hello, I cant seem to understand why I cant pass any values with the following code:

<div class="menu">
Por favor seleccione os conteúdos:
<form name="Categorias" action="Elementos_Descritivos.php" method="post">
<?php 

$Categorias = array ("Nome", "Data", "Cliente", "Observacoes");

 foreach( $Categorias as $key => $value){

echo "<div class=\"cb-row\">
      <label for=\"$value\">$value:</label>
      <input id=\"$value\" $value=\"$value\" type=\"checkbox\" value=\"$value\" checked />
      </div>";
}
 ?>
   <div class="submit">
    <input type="submit" value="Seguinte" />
</div>
    </form>
</div>
 </div>

In the Elemento_Descritivos.php page All the code i have is:

<?php

 print("<pre>");
 print_r($_POST);
 print("</pre>");

?>

It simply outputs:

Array ( )

Thank you.

+7  A: 

You need to set the name attribute on all your inputs for a form post to work. The ID is not posted when a form is submitted.

 <input id=\"$value\" name=\"$value\" .../>

Do the same for your submit button. It will allow you to figure out which submit button was pressed in case you have many in the same form.

Wadih M.
When the servers outputs to the browse what im effectively getting is name="Nome" is it not? When it was all plain html it worked flawlessly, do you mean that my submit button needs a "name" argument?
Marvin
I just have this one form, its all I got on that page. Hence why im still confused on this issue, im sorry but could you please explain a bit further? Thank you.
Marvin
Look at the HTML you're outputting - you'll end up with Nome="Nome" instead of name="Nome"
Greg
I do get Nome="Nome" but why is that stopping me from seeing anything when i post to the next page since im trying to see all of the values in $_POST, this might sound stupid but im really hitting a wall on this one. Thanks
Marvin
-_- duh! Sorry everyone I got it... Thank you all for your time, I deserve pain in coders hell.
Marvin
http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2
Milen A. Radev
+2  A: 

As Wadih pointed out - you need to assign a name attribute to your inputs. I've rewritten your code in hopes it becomes a little more clear what is going on. I've also removed the attribute $value=\"$value\".

<div class="menu">
    Por favor seleccione os conteúdos:
    <form name="Categorias" action="Elementos_Descritivos.php" method="post">

    <?php 
    $Categorias = array ("Nome", "Data", "Cliente", "Observacoes");
    foreach( $Categorias as $category){
    ?>

    <div class="cb-row">
     <label for="<?=$category;?>">
      <?=$category;?>
     </label>
     <input
      id="<?=$category;?>"
      name="<?=$category;?>"
      type="checkbox"
      value="<?=$category;?>"
      checked
      />
    </div>

    <?
    } //foreach
    ?>

    <div class="submit">
     <input name="categories" type="submit" value="Seguinte" />
    </div>
    </form>
</div>
Eddy
Thank you, I feel pretty dumb as it was quite clear in the first post. I appreciate you taking your time to rewrite the code its quite interesting to see different approaches :)
Marvin
Happy to help, lets me procrastinate from doing what I should be doing while feeling like I've done something :)
Eddy
Eddy, remove the top '<?php' and bottom '?>' tags that surround your markup, as you can't directly put html markup in a php codeblock. The example you posted won't work.
Wadih M.
oops, didn't notice the extra tags in there, removed them for posterity.
Eddy