-1

I've got a set of checkboxes but one shuld be always checked, I want that this checkbox is also hidden from my page, I need this beacause I have the DB that can be read only if this attribute is checked.. Please I need answers where you teach me how to hide the checkbox and NOT where you say that I can change the comand on SQL

$sqlPers= "SELECT * FROM personalizzazione";
$countPers = 0;
foreach ($dbh->query($sqlPers) as $rowPers){
    $selez = '';
        if($rowPers['Condimento'] == 'Base')
            $selez = " checked = 'checked' ";
    echo "<div class='checkbox'><label class='bianco'><input type='checkbox'" . 
          $selez . "name='".$rowPizza["idPizze"]."_".$rowPers["idPersonalizzazione"].
          "' id='".$rowPizza["idPizze"]."_".$rowPers["idPersonalizzazione"]."'>".
          $rowPers['Condimento']."&nbsp;(&euro; &nbsp;".$rowPers['Prezzo'].")</label></div>";
          $countPers++;
}

I tried to add type = 'hidden' in the variable $selez but nothing

Otherwise can someone say me how to disable it or hide it throught ccs? I've tried searching on internet and there weren't full solutions for my question

Biondo
  • 29
  • 1
  • 1
  • 4

3 Answers3

3

There are two things you can do:

Method 1 - Hide it using CSS:

<input type='checkbox' style='display:none;'>


Method 2 - Use a hidden type instead of checkbox:

Another option would not have a checkbox, but rather have a separate hidden input tag.

<input type='hidden' name='pizza' value='value'>
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
Stephen Cioffi
  • 1,161
  • 2
  • 13
  • 32
  • This is the best answer and I was going to write the same. You can then change the `display: none;` with JS. – matiaslauriti Jun 19 '16 at 00:56
  • This checkbox is generated form the DB, your answer makes only the checkbox hide, I need to hide also the description – Biondo Jun 19 '16 at 01:15
  • @Biondo Easy! Just apply the `style='display:none;'` to the `
    ...
    ` rather than the ``
    – Stephen Cioffi Jun 19 '16 at 01:17
  • @StephenCioffi you are saying that I should move the variable $selez in div and the game is done? I'll try it tomorrow because It's 3:30 am in Italy I need to sleep – Biondo Jun 19 '16 at 01:24
  • @Biondo No, leave `$selez` where it is. That code is the part that is actually checking off the box. To hide the checkbox and description, simply change `
    ` to `
    – Stephen Cioffi Jun 19 '16 at 01:27
  • @StephenCioffi but doing so it will hide all the checkboxes, I need to hide only one not all the set.. Correct me if I'm wrong – Biondo Jun 19 '16 at 10:31
  • @StephenCioffi I solved using a new variable setted after an if were i gave the 'display:none' if it was in true and ' ' if it was wrong.. I know that is not right doing so much mess but it is the only way I could without touching all the rest of my code ;) – Biondo Jun 19 '16 at 10:48
-1

Look at the HTML that gets sent to the browser: You have already set type='checkbox' in your string, so having an additional type='hidden' may not give you what you want. You end up with <input type='checkbox' type='hidden'...

Your error was tough to see because you're writing your code in a way that is really difficult to debug. It's better practice to do all the logic up front. You're better off building all the attributes in code first. For example:

//run test to make $hide true for the loop where you want to hide the checkbox
$type = 'checkbox';
$style = $hide? "display:none" : "";
$name = $id = "$rowPizza[idPizze]_$rowPers[idPersonalizzazione]";
...

//then later when you can build your html

$html = "<div class='checkbox'><label class='bianco'><input type='$type' name='$name' id='$id' style='$style'>";

//Finally once you've built the whole html you can echo it
echo $html;

Note that we set display:none style when the $hide variable is true. Otherwise, the style will just be an empty string and the checkbox will be shown

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
  • You are absolutely right but the checkbox is always on the display, I want that this checkbox disappears from my display – Biondo Jun 19 '16 at 00:51
  • Please leave feedback when you downvote so people trying to help can figure out how to improve their answers. – BeetleJuice Jun 19 '16 at 01:05
  • I can't, I'm new and I have to get points on this forum for leave a feedback – Biondo Jun 19 '16 at 01:10
  • @Biondo, I don't think Patrick was talking to you. this looks like a good answer and the only reason I can see why it might get downvoted is because technically there's a syntax error missing a semicolon `;` at the end of this line `$html = "...` – Jeff Puckett Jun 19 '16 at 02:06
  • There, [I added the semi-colon](http://stackoverflow.com/posts/37902955/revisions). Looks good to me now. – Jeff Puckett Jun 19 '16 at 02:10
-1

You can hide it with style="display:none;" But since it is hidden, does it have to be a checkbox?
A regular hidden input would do the job, too.

Marat
  • 617
  • 5
  • 12