• 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 - 1 through 15 (of 39 total)
  • You haven’t completely output the checkbox if you didn’t output the checked attribute. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input#htmlattrdefchecked

    Not sure how this is a WordPress question ?
    See attached – your missing the word ‘checked’ in your input :
    https://ibb.co/zGnZfS4
    So add that into your loop.

    Why are some checkboxes showing ‘TRUE’ while others are showing ‘1’ ?

    Thread Starter hsysgrp

    (@hsysgrp)

    Thank you for your response. I was testing and tried both TRUE and 1.
    If I add check to the code, it checks the box. I want to find out if the box is already checked, not check it.

    echo “<tr><th>All Those Books</th><td><input type=checkbox name=AllThoseBooks value='”.$row->AllThoseBooks.”‘></td></tr>”; I think I need to add “TRUE” to the value attribute, but am having trouble with the syntax.

    ???
    I’m not sure why you are providing this echo statement if you don’t want to check the box to On if the value is True.

    See attached – you *already know* if the box is checked or not, it’s in your array, right here :
    [CivicActionGroups] => TRUE

    https://ibb.co/JqtDjk9

    Thread Starter hsysgrp

    (@hsysgrp)

    Thank you. The desired sequence is:
    Data is in the table.
    this page displays the data for a member ID.
    The data is edited for a member.
    As you note, [CivicActionGroups] => TRUE , but the box is not checked on the form.
    Perhaps the member does not want to be in Civic Action Groups anymore, and I would edit the record to uncheck the box, but the box does not reflect the data in the table, and my issue is how do I get the box to display the checkmark if the data is true? I don’t know how to edit
    value=’”.$row->AllThoseBooks.”‘ to include value=”TRUE” which I suspect is the problem

    Yes.
    You need to insert the string checked into the input type here if true :
    echo “<tr><th>All Those Books</th><td><input type=checkbox checked name=AllThoseBooks value='”.$row->AllThoseBooks.”‘></td></tr>”;

    So you will need a conditional statement for this.

    Thread Starter hsysgrp

    (@hsysgrp)

    I’m sorry to be so slow to realize that a checkbox in this environment does not automatically display a true value as a checkmark. I am migrating a db from an Access
    system where a checkbox automatically displays if true. Can you point me to an example of how I combine this condition of value = “TRUE” with echo “<tr><th>All Those Books</th><td><input type=checkbox checked name=AllThoseBooks value=’”.$row->AllThoseBooks.”‘></td></tr>”;

    You’re not really showing us enough of your loop to be accurate – it depends on what sort of loop you are already using tbh.
    But rudimentarily, just loop through your array :
    https://www.php.net/manual/en/function.in-array.php
    or :
    https://www.php.net/manual/en/function.array-intersect.php

    if(in_array('AllThoseBooks', $_POST['ArrayName'])){
      $checktrue='checked'
    }

    Then in your input echo, you could just always output $checkedtrue :
    echo '<tr><th>All Those Books</th><td><input type=checkbox ' . $checkedtrue . ' name=AllThoseBooks value='".$row->AllThoseBooks."'></td></tr>";

    But – this will work, but it may not be the most efficient approach.

    Also, if you are getting this error, it’s bc your concatenation delimiters are incorrect :

    Parse error: syntax error, unexpected ‘checked’ (T_STRING), expecting ‘,’ or ‘;’ in /home4/hsysgrpc/public_html/wp-content/themes/twentytwelve-child/custom-page_Members_Desc.php on line 88

    Just double check your strings :
    https://www.php.net/manual/en/language.operators.string.php

    echo '<tr><th>All Those Books</th><td><input type=checkbox name="AllThoseBooks" value="AllThoseBooks" '. checked( in_array("AllThoseBooks", $row) . ' /></td></tr>';
    but I don’t know that $row is an array

    Thread Starter hsysgrp

    (@hsysgrp)

    We’re getting close..yes, it’s an array, foreach($retrieve_data as $row){$row…..
    first echo works but no check; second echo has the conditional statement and gets an error line 89 shouldn’t /> be just > as it closes the input?

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

    Try this :

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

    Thread Starter hsysgrp

    (@hsysgrp)

    Warning: in_array() expects parameter 2 to be array, string given in /home4/hsysgrpc/public_html/wp-content/themes/twentytwelve-child/custom-page_Members_Desc.php on line 90
    All those Books and the checkbox displays, but no check.

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

    • This reply was modified 3 years, 10 months ago by corrinarusso.
Viewing 15 replies - 1 through 15 (of 39 total)
  • The topic ‘Checkbox not checked’ is closed to new replies.