Jump to content

Recommended Posts

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 by Foracc
<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.

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?

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;

 

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>';

 

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.