Foracc Posted November 9, 2021 Share Posted November 9, 2021 (edited) Hello there, I have a problem with the checkboxes. When the page is loaded A text file is to be evaluated. If the Line 2 = "Line 2" then the checkbox from Checked should be marked <?php $Datei = file('test.txt')[2]; if(Datei = "Line 2"){ //Here should be the Checkbox marked as Checked // should here come the html code ?? <input type="checkbox" name="Checkbox" checked='checked' value="checkbox">Checkbox</input> }else{ //Here should be the Checkbox as non Checked } ?> <input type="checkbox" name="Checkbox" checked='checked' value="checkbox">Checkbox</input> Edited November 9, 2021 by Foracc Quote Link to comment Share on other sites More sharing options...
ginerjm Posted November 9, 2021 Share Posted November 9, 2021 <input type="checkbox" name="Checkbox" checked value="true">Checkbox</input> All you need is 'checked' to set it. Plus it is good to set a value such as 'true' or something indicative of which checkbox has been selected. PS - in php I find it easier to use all lowercase names to avoid mis-types and wondering why something is not being recognized. Remember - if a checkbox is not checked, then $_POST['Checkbox'] will not show up. Quote Link to comment Share on other sites More sharing options...
Foracc Posted November 9, 2021 Author Share Posted November 9, 2021 32 minutes ago, ginerjm said: <input type="checkbox" name="Checkbox" checked value="true">Checkbox</input> All you need is 'checked' to set it. Plus it is good to set a value such as 'true' or something indicative of which checkbox has been selected. PS - in php I find it easier to use all lowercase names to avoid mis-types and wondering why something is not being recognized. Remember - if a checkbox is not checked, then $_POST['Checkbox'] will not show up. hey thanks for the reply. Do I build the html into the PHP code? or can you change the HTML code via PHP? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted November 9, 2021 Share Posted November 9, 2021 You can send the html code from your php code and then use JS to check it from a trigger on your web page if you wish. Need to set an id on the input tag to do that. document.getElementById('chkbox').checked = true; Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 9, 2021 Share Posted November 9, 2021 In your if/then results, create a variable that will determine the checked status of the checkbox. I typically ise a ternary operator for these for brevity. Also, I would avoid using a name like "checkbox", give it a name as to what it is intended for. $Datei = file('test.txt')[2]; $checkboxChecked = ($Datei == "Line 2") ? 'checked' : ''; //If true, set value to 'ckecked' else value is empty string echo "<input type='checkbox' name='Checkbox' value='checkbox' {$checkboxChecked}>Checkbox</input>'; Quote Link to comment Share on other sites More sharing options...
Phi11W Posted November 10, 2021 Share Posted November 10, 2021 Or even something like this: $Datei = file('test.txt')[2]; printf( '<input type="checkbox" name="Checkbox" %s value="Y">Checkbox</input>' , ( ( "Line 2" == Datei) ? 'checked' : '' ) ); "Danger, Will Robinson!" Note my change to your "equality test" involving Datei: your original code would have "misbehaved" in interesting ways being, as it was originally given, an assignment! Regards, Phill W. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.