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.
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’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.
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
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.
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