0

I have an automatic check system that is not working properly

while($linha = mysql_fetch_array($tabela))
{
    $nome = $linha['TABLE_NAME'];

    ?>

        <input type="checkbox" name="<?php echo $nome ?>" id="<?php echo $nome ?>" value="<?php echo $nome ?>" <?php if(isset($_POST[$nome]))  echo "checked='checked'";?>/> <?php echo $nome ?><br />

    <?php
}

The problem is the $nome variable, it have spaces, like 'A B' or '106 South', doing the system not works correctly... I don't know how to solve it, any help will be welcome!

3 Answers3

2

The problem lies with the fact that you're using spaces in the name attribute of the input tag. See this reference:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

This has also been addressed in other SO questions

Spaces are not allowed and will not work properly when you submit them using either POST or GET methods. You should therefore rename any values with spaces or use a str_replace function to substitute the spaces with underscores or something similar. Also note the part that said "must begin with a letter"... this also means that a value of 106 Sul is invalid even if you changed it to 106Sul.

Community
  • 1
  • 1
sjagr
  • 15,983
  • 5
  • 40
  • 67
0

if the space is the problem, cant you just replace it with another character, like this:

str_replace(' ','|',$nome);

Then when reading the results, you just replace the | with the space and save it to the DB

-1

You can check an input only with the "checked" value in the tag:

<input type="checkbox" checked />

Try to write the word instead of "checked='checked'" in your html from php.

Alex
  • 715
  • 5
  • 14
  • -1 because `checked="checked"` is still valid HTML markup even though it is not common for HTML5, it is alive and kicking for XHTML standards. It will work in any modern browser either way. – sjagr Oct 21 '14 at 18:57
  • I didn't mention you can't use it. – Alex Oct 21 '14 at 19:22
  • However it won't solve the problem, and thus isn't an answer but should rather be a comment. – sjagr Oct 21 '14 at 19:27