-
My code is easy. It loops through, uses the $_POST[VALUEHERE] as a variable, then saves that as ' checked', if the checkbox was in the form.
But either way, the code should work if you just use this line:
<input type="checkbox" name="checkbox1" id="checkbox1" checked="'.$checkbox1.'"/>
-
Ok status update.
Tried your script tech_support and it generates 50 checkboxes in a row. I was probably a bit unclear, I have checkboxes all around my form and the status of these needs to be written to the formsaved.html. Right now I am fiddling with the solution from djr33. The result written to the saved form regardless of if checkbox1 is checked or not is
checked=""
It seems checked="checked" is never written and that checked="" is the same as checked in html code?? Something wrong at least.
Thanks for your help guys. Php is really fun but a bit frustrating sometimes
:)
My current code is
PHP Code:
<?php
$file = fopen("formsaved.html", "w") or exit("Unable to open file!");
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==1) { //and it is checked
$$key = ' checked'; //set $$key, meaning $checkbox#, to checked
}
} //end ifs/foreach
}
//do your code below for the form, using explicit variables
//like $checkbox34
$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"/>
<br />
<input type="checkbox" name="checkbox1" id="checkbox1" checked="'.$checkbox1.'"/>
<br />
<input type="submit" name="button" id="button" value="Save" />
</form>
<body>
</html>
'
;
fwrite($file, $stringData);
fclose($file);
?>
-
Trying a new approach on my own but there is something wrong in the code. Anyone help me?
PHP Code:
<?php
$file = fopen("formsaved.html", "w") or exit("Unable to open file!");
function checkbox($checkbox)
{
(if $_POST[$checkbox.]==1) echo 'checked="checked"';
}
//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"'checkbox(checkbox1);'/>
<br />
<input type="submit" name="button" id="button" value="Save" />
</form>
<body>
</html>
'
;
fwrite($file, $stringData);
fclose($file);
header("Location: formblank.html");
?>
-
PHP Code:
function checkbox($checkbox)
{
(if $_POST[$checkbox.]==1) echo 'checked="checked"';
}
No need for the dot after $checkbox
PHP Code:
"'checkbox(checkbox1);'/>
checkbox1 should be just 1
-
But it needs to be checkbox1 because the checkbox element is called this?
There is something bad in my code since it doesn't execute the code at the bottom (forward to page). Any clues?
PHP Code:
<?php
$file = fopen("formsaved.html", "w") or exit("Unable to open file!"); //öppnar filen
function checkbox($checkbox)
{
(if $_POST[$checkbox]==1) echo 'checked="checked"';
}
//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"'checkbox(checkbox1);'/>
<br />
<input type="submit" name="button" id="button" value="Save" />
</form>
<body>
</html>
'
;
fwrite($file, $stringData);
fclose($file);
header("Location: formblank.html");
?>
-
if ($value==1) { //and it is checked
try changing that to
if ($value=='checked') { //and it is checked
Since you are using 'checked', rather than '1', as I am used to, with checkboxes.
-
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");
?>