Checkbox needs to reflect the database value

Another newbie to PHP… ugh.
This has got to be so simple, but I’ve tried dozens of combinations after searching forums, google, etc.

On my pullclient.php page, I build an HTML table from a database table, and insert a column with a checkbox. I have built the code that will update the underlying ‘Selected’ field to 0 or 1 when the checkbox is clicked. The checkbox has onclick=“submit()” to call a setselected.php page, which does the update and then reloads my pullclient.php page.

Problem is, when the pullclient.php page reloads all the checkboxes are empty even though some of the db fields may be set to 1. Here is the code that builds the table and inserts the checkbox.

<?php while ($row = mysqli_fetch_array($query)) { //$cid=$row['Selected']; //if($cid == 1){echo "Checked";} else{echo "Unchecked";} echo ' '.$row['ClientID'].' '.$row['FirstName'].' '.$row['LastName'].' '.$row['Email'].' '.$row['KDWP_Nbr'].' '. date('F d, Y', strtotime($row['DOB'])) . ' '.$row['DriverLicenseNbr'].' '.$row['PrimaryPhone'].' '.$row['SecondaryPhone'].' '.$row['Address1'].' '.$row['Address2'].' '.$row['City'].' '.$row['State'].' '.$row['ZipCode'].' '.$row['ClientNotes'].' '; }?>

I have tried several variations inside the tag, but have not been successful.

Please help?
Thanks.
Keith

Hi okiearcher welcome to the forum

Is the PHP code giving the input(s) the checked attribute?

To make a checkbox checked by default, you simply give it the checked attribute.

No it isn’t. When I tick the box, the table field “Selected” for the correct record in the database is set to value of 1. But when the page reloads or is first loaded, all the checkboxes are not ticked.

I tried setting a variable $val like this:
$cid=$row[‘Selected’];
if($cid == 1){$val=“Checked”;}else{$val=“”;}

Then added this to my checkbox input tag:
…onclick=“submit()” <?php echo $val; ?>>

But the box is still unchecked, and I get an extra > displayed on my page beside the checkbox.

I don’t understand this line of code:

<form action="setselected.php" method="post"><input type="hidden" name="Selected" value="0,'.$row[0].'"><input type="checkbox" name="Selected" value="1,'.$row[0].'" onclick="submit()"></form></td>

Specifically, you’ve got a hidden input and a checkbox which are both called “Selected”. Doesn’t that confuse matters?

I’m not sure why your code to display the checked attribute is causing the problem you describe, could you repost the whole thing showing it in situ?

I’ve come back to this problem. I have modified the code, cleaned it up some and still cannot get the <?php echo $stat; ?> to insert the word ‘checked’. The last cell in the table accurately displays ‘checked’ as set in the variable, and in the correct row. I just can’t for the life of me get the echo to display the value of the variable.

<?php while ($row = mysqli_fetch_array($query)) { $stat=""; $sel=$row['Selected']; if($sel == 1){$stat="checked";} echo ' '.$row['ClientID'].' >
				<td>'.$row['FirstName'].'</td>
                <td>'.$row['LastName'].'</td>
				<td>'.$row['Email'].'</td>
				<td>'.$row['KDWP_Nbr'].'</td>
                <td>'. date('F d, Y', strtotime($row['DOB'])) . '</td>
				<td>'.$row['DriverLicenseNbr'].'</td>
				<td>'.$row['PrimaryPhone'].'</td>
				<td>'.$row['SecondaryPhone'].'</td>
				<td>'.$row['Address1'].'</td>
				<td>'.$row['Address2'].'</td>
				<td>'.$row['City'].'</td>
				<td>'.$row['State'].'</td>
				<td>'.$row['ZipCode'].'</td>
				<td>'.$row['ClientNotes'].'</td>
				<td>'.$stat.'</td>
            </tr>';
    }?>

The output looks like this when viewing the page source:

61 Keith xxxxxxxxxx xxxx@gmail.com xxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxxx xx xxxxx checked
<td>61</td>
					<!--<td><form action="deleteclient.php" method="post"><input type="hidden" name="Selected" value="0,61"><input type="checkbox" name="Selected" value="1,61" onclick="submit()" <?php echo $stat; ?>></form></td>-->
					<td><form action="" method="post"><input type="checkbox" name="Selected" value="1,61" <?php echo $stat; ?></form></td>
					<td>Keith</td>
                      <td>xxxxxxxxxx</td>
					<td>xxxx@gmail.com</td>
					<td></td>
                    <td>xxxx</td>
					<td></td>
					<td>xxxxxxxxx</td>
					<td>xxxxxxxxx</td>
					<td>xxxxxxxxx</td>
					<td></td>
					<td>xxxxxxxxxx</td>
					<td>xx</td>
					<td>xxxxx</td>
					<td></td>
					<td>checked</td>

You should not be seeing PHP code in the output view-source
I think you are running afoul with “inside single quotes are unprocessed literals, double quotes get processed, output needs double quotes, concatenate - backslash escape, everything looks a mess, very confusing”
Personally, when I write code that has a lot of “going in and out of PHP” I prefer to use Heredoc

http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Thanks for the input. Yes, I am sure it’s a mess. I am brand new to php. Please forgive my ignorance.

Don’t be discouraged by the messiness, that comes part and parcel due to PHP outputting HTML
The is no way to avoid it entirely, only to make it “better” to some degree.

It is quite normal to learn by getting “simple” to work and then continuing on with similar as things get more complex. And to a very large extent when to change tactics depends mostly on personal preference.

For example, if I learn to do

if ($foobar === 1) { 
 $var = "one"; 
} 

then later I wanted to also test for $foo, $bar, $baz, $qux, $quux, $quuz etc. it would be normal for me to just keep writing more if blocks. Until I discover switch case, that is. Then it’s up to me on how many ifs there are before I change to using switch case instead.

Similar with what I call “going in and out of PHP”
If there’s a lot of PHP code I might echo an HTML tag. If there’s a lot of HTML code, I might <?php echo $var; ?> inside of it.
But when there’s a lot of PHP code mixed in with a lot of HTML code I find that using heredoc helps keep things more readable and easier to work with.

I’d agree, specifically this section of code:

 echo '<tr>
    <td>'.$row['ClientID'].'</td>
    <td><form action="" method="post"><input type="checkbox" name="Selected" value="1,'.$row[0].'" <?php echo $stat; ?>></form></td>
    <td>'.$row['FirstName'].'</td>

You’re using single-quotes to surround the parameters that you send to echo() here, you close them to insert the value of $row[0], open them again, output a double-quote, but then stick a php echo() without closing the single-quotes as you have elsewhere. I’d think changing that piece to read

 echo '<tr>
 <td>'.$row['ClientID'].'</td>
 <td><form action="" method="post"><input type="checkbox" name="Selected" value="1,'.$row[0].'"' . $stat . '></form></td>
 <td>'.$row['FirstName'].'</td>

might do it. But it’s still confusing code.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.