View Full Version : Save form script. Problem
lindm
07-09-2007, 09:41 PM
I am a beginner and building a form processor script that is intended to make a backup of the submitted form, in html format. I might be using the wrong technique, but here is a simple setup of my processor script (writeform.php). My problem is the checkboxes...how can I reproduce a checked checkbox within the $stringData, which depends on if the submitted checkbox is checked?
<?php
$file = fopen("formsaved.html", "w") or exit("Unable to open file!");
$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="check" id="check" />
<br />
<input type="submit" name="button" id="button" value="Save" />
</form>
<body>
</html>
'
;
fwrite($file, $stringData);
fclose($file);
?>
djr33
07-09-2007, 10:37 PM
$ifchecked = ''; //initialize to blank
if ($_POST['check'] == 1) $ifchecked = 'checked ';
'.....<input type="checkbox" name="check" id="check" '.$ifchecked.'/>.....'
That will add ... checked /> to the checkbox if the value of the submitted checkbox was 1 (checked). Or, it will just leave as is.
lindm
07-10-2007, 07:39 AM
$ifchecked = ''; //initialize to blank
if ($_POST['check'] == 1) $ifchecked = 'checked ';
'.....<input type="checkbox" name="check" id="check" '.$ifchecked.'/>.....'
That will add ... checked /> to the checkbox if the value of the submitted checkbox was 1 (checked). Or, it will just leave as is.
Perfect thanks. Added a small adjustment so that it would work (the checked=). Next question...say I have 50 checkboxes in a form...is there an easier way to perform the check than having to setup a $ifchecked1, $ifchecked2, ... $ifchecked50?
CURRENT STATE
<?php
$file = fopen("formsaved.html", "w") or exit("Unable to open file!");
$ifchecked = ''; //initialize to blank
if ($_POST['check'] == 1) $ifchecked = 'checked ';
$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="check" id="check" checked="'.$ifchecked.'"/>
<br />
<input type="submit" name="button" id="button" value="Save" />
</form>
<body>
</html>
'
;
fwrite($file, $stringData);
fclose($file);
?>
tech_support
07-10-2007, 07:49 AM
Try this:
$ifchecked = array();
$checkboxes = '';
for ($i=1; $i < 51; $i++) {
if ($_POST['check'.$i] == $i) {$ifchecked[$i] = 'checked ';}
else { $ifchecked[$i] = ''; }
$checkboxes .= '<input type="checkbox" name="check'.$i.'" id="check'.$i.'" checked="'.$ifchecked[$i].'"/>'."\n";
}
$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 />
'.$checkboxes.'
<br />
<input type="submit" name="button" id="button" value="Save" />
</form>
<body>
</html>
'
;
fwrite($file, $stringData);
fclose($file);
?>
djr33
07-10-2007, 07:50 AM
You could do a foreach loop using $_POST, and then check if the name is checkbox____.
<?php
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
?>
tech_support
07-10-2007, 07:53 AM
$$key?
djr33
07-10-2007, 07:54 AM
See the comments I added (edited).
$$key is the variably-named variable, such as $checkbox5, when $key == 'checkbox5'.
lindm
07-10-2007, 08:59 AM
Can't seem to get it to work...see my code below. The regular post contents works, but not the checkbox status.
<?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="'.$$key.'"/>
<br />
<input type="submit" name="button" id="button" value="Save" />
</form>
<body>
</html>
'
;
fwrite($file, $stringData);
fclose($file);
?>
djr33
07-10-2007, 09:01 AM
Not $$key in the form, but the explicit name of the variable, $checkbox1, then $checkbox2, etc.
$$key will have the value of $[$key], meaning $last_post_variable_checked, so, probably $button, which is not what you want.
tech_support
07-10-2007, 09:01 AM
Have you tried the one I posted?
//Me just confused at djr33's one :p
djr33
07-10-2007, 10:31 AM
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.'"/>
lindm
07-10-2007, 05:29 PM
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
$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);
?>
lindm
07-12-2007, 07:20 AM
Trying a new approach on my own but there is something wrong in the code. Anyone help me?
<?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");
?>
tech_support
07-12-2007, 07:22 AM
function checkbox($checkbox)
{
(if $_POST[$checkbox.]==1) echo 'checked="checked"';
}
No need for the dot after $checkbox
"'checkbox(checkbox1);'/>
checkbox1 should be just 1
lindm
07-12-2007, 07:49 AM
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
$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");
?>
djr33
07-12-2007, 09:17 AM
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.
lindm
07-12-2007, 09:52 AM
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
$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");
?>
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.