• This testing page selects a single record from a custom table based on an ID. The data is text and checkboxes. The text data displays correctly and the echo sql statement reports the checkbox data returns TRUE, FALSE, 1 or 0. However none of the checkboxes show a check. ??
    Code below displays a value for the OtherWriteIn textfield
    echo "<tr><th>List Your Organizations</th><td><input type=text name=OtherWriteIn value='".$row->OtherWriteIn."'></td></tr>";
    Code below does not display a check for the checkbox field
    echo "<tr><th>All Those Books</th><td><input type=checkbox name=AllThoseBooks value='".$row->AllThoseBooks."'></td></tr>";

    The page I need help with: [log in to see the link]

Viewing 15 replies - 16 through 30 (of 39 total)
  • Thread Starter hsysgrp

    (@hsysgrp)

    Warning: in_array() expects parameter 2 to be array, object given in /home4/hsysgrpc/public_html/wp-content/themes/twentytwelve-child/custom-page_Members_Desc.php on line 91
    same result, All those Books and the checkbox displays, but no check.
    Where is the value being “TRUE” being passed? I guess I am not understanding where in the code the logic of
    If value = “true”, check box, if not, not being enabled?
    Thank you for taking all this trouble.

    Yes. I made a mistake earlier.

    Use this :
    echo '<tr><th>All Those Books</th><td><input type="checkbox" name="AllThoseBooks" value="AllThoseBooks" ' . checked( in_array("AllThoseBooks", $row)) . ' /></td></tr>';

    not this :
    echo "<tr><th>All Those Books</th><td><input type=checkbox name=AllThoseBooks value='".$row->AllThoseBooks."'></td></tr>";

    Yes, it not easy to program like this! Lol

    You can see here what the second paramter should look like – which you have in $row

    https://www.w3schools.com/php/func_array_in_array.asp

    Thread Starter hsysgrp

    (@hsysgrp)

    Good Morning, I’m back.
    echo ‘<tr><th>All Those Books</th><td><input type=”checkbox” name=”AllThoseBooks” value=”AllThoseBooks” ‘ . checked( in_array(“AllThoseBooks”, $row)) . ‘ /></td></tr>’;
    produces:
    Warning: in_array() expects parameter 2 to be array, object given in /home4/hsysgrpc/public_html/wp-content/themes/twentytwelve-child/custom-page_Members_Desc.php on line 91
    parameter 2 is an array? of the fields? ie, ID, FirstName, etc. Should we be examining the values of the fields somehow?

    It’s getting very tedious to try to program by only seeing a tiny snippet of your entire code. Can you dump the entire file contents into pastebin pls :
    https://pastebin.com/
    Be sure to remove all references to usernames, password, database names, etc obviously.

    Thread Starter hsysgrp

    (@hsysgrp)

    I just pasted it. Thank you for your patience.

    Lol. Yes, but then you need to share the link to it!

    Thread Starter hsysgrp

    (@hsysgrp)

    Sorry, first time. link is hsysgrp, DebugDutchess2021 name of paste is checkbox.

    Hi @hsysgrp

    You will need to provide a hyperlink URL that would have been provided to you from pastebin.com

    Thread Starter hsysgrp

    (@hsysgrp)

    Hope this is what you need…
    https://pastebin.com/Q1LiSz3Q

    Thread Starter hsysgrp

    (@hsysgrp)

    Was that the correct link?

    Yes.

    You are going to have to check each checkbox value in it’s own array.
    Inside this loop :
    foreach($retrieve_data as $row){
    add all your checkboxes into an array value like this :

    $checkboxes = array(
    'AdventuresEnSoleil'	
    'Bridge'	
    'Canasta'
    'ContemporaryLit'	
    'Cuisine'
    );

    Then just use another foreach to print out all the boxes like this :

    foreach ( $checkboxes as $cb ) {
                $checked = ( $row->$cb == '1' ) ? 'checked' : '';
                echo '<tr><th>LOOP ' . $cb . '</th><td><input type="checkbox" name="' . $cb . '" value="' . $cb . '" ' . $checked . ' /></td></tr>';
    }

    Putting it all together will be like this :
    https://pastebin.com/LAw3cf62

    Thread Starter hsysgrp

    (@hsysgrp)

    I inserted your code, added commas to the array, and changed the data from “TRUE” to “1” and am looking at little blue check marks! thank you, I would never have figured out that syntax on my own.

    ` echo “<table>”;
    echo “<tbody>”;
    $checkboxes = array(
    ‘AllThoseBooks’,
    ‘ArtOnTheGo’,
    ‘AdventuresEnSoleil’,
    ‘Bridge’,
    ‘Canasta’,
    ‘ContemporaryLit’,
    ‘Cuisine’
    );
    foreach( $checkboxes as $cb) {
    $checked = ($row->$cb == ‘1’) ? ‘checked’: ”;

    echo ‘<tr><th>’. $cb .'</th><td><input type=”checkbox” name=”‘. $cb .'” value=”‘ . $cb .'” ‘ . $checked . ‘ /></td></tr>’;

    }`

    Great!
    If you want to add in the headings into the checkboxes, i.e.
    Ties with Community Organizations
    it will get a little trickier. You would need to put conditional loops that check for the first value of the checkbox array, and insert the heading.
    such as,
    if $cb == ‘CivicActionGroups’
    echo ‘Ties with Community Organizations’

    Or, you could add these headings to you select statement.

Viewing 15 replies - 16 through 30 (of 39 total)
  • The topic ‘Checkbox not checked’ is closed to new replies.