I am trying to have a html form with checkboxs. I then have php to validate the form and get the value for the check box. If the checkbox corresponds to a certain text then there will be a fwrite statment.
Here is the code for the html checkboxs:
<input class="choose" type="checkbox" name="check_list[]" value="Python"> Python</label>
<input class="choose" type="checkbox" name="check_list[]" value="Java"> Java And Html(opitnal)</label>
<input class="choose" type="checkbox" name="check_list[]" value="PHP"> PHP And Html(optinal)</label>
And here is the PHP form validation:
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $selected) {
$lan = $selected;
}
}
And Here is the if statment:
$fh = fopen("myfile.php", "w") or die("Error! :(");
if ($lan == "Python") {
$page1 = "Some Code For The Fwrite";
fwrite($fh, $page1);
}
The fopen works but the fwrite does not actually write to the file, it should write the code in the if statment to file.
Also The form validation seems to work as if i echo the check box it returns the correct thing!
check_list[]
is an array which means if the user checks them all the $lan
will not equal to python
or java
but will be equal to the last element of the array in your case is PHP
, your fwrite won't work if more than one checkbox is checked
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $selected) {
$lan = $selected; //$lan will equal to the last value of the array, if more than one checkbox is ticked
}
}
you can open your file inside your if
statement, this code will only work if python is checked
if($lan == 'Python'){
$file = fopen("myfile.php","w") or die("Error! :(");
$page1 = "Some Code For The Fwrite again";
fwrite($file,$page1);
fclose($file);
}