I'm rebuilding an old MS Access
interface into a new PHP
interface. The backend database is SQL Server
and I'm trying to pull data from that database to the html form. So my form is filled out with all it's text input fields to the same name and ID as the column in the database table. I then have this snippet I wrote in the bottom.
<script type="text/javascript">
var mem = JSON.parse('<?php echo json_encode($vars); ?>');
$.each( mem[0], function( key, val) {
$("#"+key).val(val);
if (key == "Sex") {
if (val == "M") {$("Sex-M").prop("checked", true);}
else if (val == "F") {$("Sex-F").prop("checked", true);}
}
if (key == "InvalidEMail") {
if (val == "1") {$("#InvalidEMail").prop("checked", true);}
else if (val == "0") {$("#InvalidEMail").prop("checked", false);}
};
}
</script>
So here's the question I have. There's got to be a better way of doing this. I'd like the JavaScript to dynamically check the checkboxes if the value is true. But according to Microsoft's SQL Server Driver for PHP a bit operator (for true and false) in SQL Server
comes out as a standard integer in PHP. So then is there no way for PHP
to be sure that this is a true/false statement instead of an actual 1 or 0?
If I tell JS
to just look for ones and zeroes and make them into true/false, I'm afraid I'll grab some bit (see what I did there?) of data somewhere that SHOULD be a literal one or zero, and try to convert it into true/false incorrectly.
This code I wrote will work for the items I specify, but won't dynamically find other checkboxes that need them to be true/false.
ADDENDUM: I have about a hundred forms to rebuild like this one. So I've decided to try to build this in a manner that would allow me to easily copy and paste a template and change as little as possible to make them function for a new form. That's why I want this to dynamically populate the data. Because this form functions as both a "New" and "Edit" form, the form itself I'd like to have empty by default. True, I could skip javascript altogether and run an if statement in each input
field ala:
<?=(isset($vars[0]['InvalidEmail']))?$vars[0]['InvalidEmail']:'';?>
but the problem would remain the same. the SQL Server
drivers for PHP convert bit
operators to integer
. That seems like a full stop problem to me; no solution but to hard code every bit
-type column as a boolean in php. But I was hoping some brilliant programmer out there as figured out a good way around this.