Im having a problem trying to prepopulate checkboxes. My aim is to be able to see which questions have been checked previously so I can update a form
here is my failed attempt.


td><input type="checkbox" name="independent" value="' . $row['independent'] . '"></td>

this is based on a thread i found on this site- but in my case I do not want 'checked' to be echoed

    by "prepopulate" i assume that you simply mean that you want the checkbox to be either checked or unchecked depending on the results from a database query. i will further assume that the "independent" field in your database table is some sort of boolean field (perhaps and integer that is either 1 or 0).

    echo '<input type="checkbox" name="independent" value="1"';
    if ($row['independent'] == 1) {echo 'checked';} echo '>';
    

      yes devinemke that is exactly it. I have implemented your suggestion but have hit a snag as somewhere. I know I must end the echo statement before any PHP code so that the only thing outputted, or echoed, is HTML stuff but I cannot see what is wrong

      td><p> Independent, self-managing organisation, incorporated </p></td>
             echo '<input type="checkbox" name="independent" value="1"'; 
      if ($row['independent'] == 1) {echo 'checked';} echo '>';
      </td>
      
        Tezread wrote:

        ...but I cannot see what is wrong

        what is wrong depends entirely on the broader logic of your code, which is not at all clear from the snippet you posted. if all of the HTML is being output within PHP tags using [man]echo[/man] then the correct syntax might be:

        <?php
        echo '
        a bunch of HTML here
        a bunch more HTML here
        
        <input type="checkbox" name="independent" value="1"'; 
        if ($row['independent'] == 1) {echo 'checked';} echo '>
        
        more HTML here
        '; 
        ?>
        

        if you are only using PHP to output just this particular bit then it might be:

        a bunch of HTML here
        a bunch more HTML here
        
        <input type="checkbox" name="independent" value="1" <?php if ($row['independent'] == 1) {echo 'checked';} ?>>
        
        more HTML here
        

        you might want to read up on the basic syntax

          Write a Reply...