Alright, now I have got a working solution (see code below). Apparently the value of a checkbox when checked is 'on'. I also changed the code since checked="" seems to be the same as checked="checked".
Thanks for all help!
PHP Code:
<?php
$file = fopen("formsaved.html", "w") or exit("Unable to open file!"); //öppnar filen
//script för att hantera checkboxes
foreach ($_POST as $key => $value) { //run through each $_POST variable, $key as the name, $value as value
if (strpos($key,'checkbox')===0) { //if the name of that variable starts with checkbox...
if ($value=='on') { //and it is checked
$$key = 'checked="checked"'; //set $$key, meaning $checkbox#, to checked
}
} //end ifs/foreach
}
//do your code below for the form, using explicit variables
//like $checkbox34
//beräkningar
$sumformat = number_format(round($_POST['1'] + $_POST['2']), 0, ',', ' ');
//data som skrivs till htmlfil
$stringData =
'<head>
</head>
<form id="form1" name="form1" method="post" action="writeform.php">
<input type="text" name="1" id="1" value="'.$_POST['1'].'"/>
<br />
<input type="text" name="2" id="2" value="'.$_POST['2'].'"/>
<br />
<input type="text" name="sum" id="sum" value="'.$sumformat.'"/>
<br />
<input type="checkbox" name="checkbox1" id="checkbox1"'.$checkbox1.'/>
<input name="checkbox2" type="checkbox" id="checkbox2" '.$checkbox2.'/>
<br />
<input type="submit" name="button" id="button" value="Save" />
</form>
<body>
</html>
'
;
fwrite($file, $stringData);
fclose($file);
header("Location: formblank.html");
?>
Bookmarks