Jump to content

Recommended Posts

Hi Everyone,

 

I'm definitely a beginner when it comes to PHP so please bare with me. I'm working on an application which uses .CSV files to store data. Long story short, I have a checkbox for each row of data with the ID as the row number. I'm trying to create a script that will save the state of the checkboxes (checked/unchecked), then when loaded it will recall the saved state. I'll do my best to create a script to better explain what I'm trying to do.

 

Here is my save script:

 

<?php
session_start();
$grade = $_SESSION['grade'];
if (isset($_POST['submit']))  { while ($checkbox <= $checkbox) { echo "Okay"; $towrite = " chk" . $_POST['checkbox']; " "; } //Checking to see if the variable was posted, then sets a variable to correspond to the checkbox.

//Will write " chk1 " if checkbox is selected 
//Want this incremented and written so on for every checkbox $_POST-ed.

//Below will write the selected checkboxes to file

$myFile = "csv/" . $grade . ".php";
echo "<p></p>";
echo "Opening " . $myFile;
$fh = fopen($myFile, 'a+') or $_SESSION['changesubmit']='fail'; 
fwrite($fh, $towrite);
fclose($fh);


}

?>

 

 

Basically I want to be able to do this execution with all checkboxes and I'll be fine. But it seems repetitive and extremely messy if I were to copy and increment the checkbox name. For each Row a checkbox incremented is named (i.e Checkbox1, Checkbox2, Checkbox3,). I want this script to save to a file with chk1 if the checkbox is selected and so on with every checkbox. I have never done something this as you could probably guess from my poor PHP example.

 

Any guidance or pre-created script to help me learn would be great.

 

Thanks, Evan

Link to comment
https://forums.phpfreaks.com/topic/266507-save-checkbox-state-in-data-on-submit/
Share on other sites

One method I tend to use when printing out a large number of variable checkboxes, is to generate them from an array and then use that array in the validation process. Something like this, in other words:

// Generate the array containing the source data, usually retrieved from a database or similar.

$testData = array ('test1' => array (1 => false, 2 => false, 3 => false, 4 => true, 5 => false, 6 => false),
				'test2' => array (1 => false, 2 => true, 3 => false, 4 => false ,5 => false, 6 => false));

$output = '';
$chbxTemplate = <<<Template
<input type="checkbox" name="test[%1\$s]" value="%2\$d">
<label for="chbx_%1\$s_%2\$d">%2\$d</label>

Template;

// Check if form has been submitted.
if (isset ($_POST['submit'])) {
foreach ($_POST['test'] as $name => $val) {
	if (!isset ($testData[$name][$val])) {
		// Not a valid input, could not have gotten this from the form.
	}

	if ($testData[$name][$val] != true) {
		// Correct data, but wrong answer.
	}
}
}

// Generate the checkboxses grouped by fieldsets.
foreach ($testData as $name => $values) {
$output .= '<fieldset><legend>'.htmlspecialchars ($name)."</legend></fieldset>\n";
foreach ($values as $val) {
	$output .= sprintf ($chbxTemplate, htmlspecialchars ($name), $val);
}
$output .= "</fieldset>\n";
}

 

As for your code, you do have some odd constructs in there. For instance $checkbox <= $checkbox will always be false, since a variable can't be anything but exactly the same as itself. You're also overwriting $towrite inside the loop, causing all but the last value to be discarded.

When it comes to saving stuff to a file, I recommend using file_put_contents () instead of the whole fopen (); fwrite (); fclose () routine.

 

Anyway, the above code should help you get on with your code. Just remember to be mindful of every little detail, and think about the why's and how's for every thing I've done. ;)

Should you need more help, just feel free to ask.

To make my question a little more simple.

 

For every checkbox that is checked, I want this script to add "chk $rownum" to $grade (file). When the checkbox isn't checked, nothing should happen. When checked the checkbox has the value of the row number it is in.

 

Hope that's better

 

Thanks, Evan J

One method I tend to use when printing out a large number of variable checkboxes, is to generate them from an array and then use that array in the validation process. Something like this, in other words:

// Generate the array containing the source data, usually retrieved from a database or similar.

$testData = array ('test1' => array (1 => false, 2 => false, 3 => false, 4 => true, 5 => false, 6 => false),
				'test2' => array (1 => false, 2 => true, 3 => false, 4 => false ,5 => false, 6 => false));

$output = '';
$chbxTemplate = <<<Template
<input type="checkbox" name="test[%1\$s]" value="%2\$d">
<label for="chbx_%1\$s_%2\$d">%2\$d</label>

Template;

// Check if form has been submitted.
if (isset ($_POST['submit'])) {
foreach ($_POST['test'] as $name => $val) {
	if (!isset ($testData[$name][$val])) {
		// Not a valid input, could not have gotten this from the form.
	}

	if ($testData[$name][$val] != true) {
		// Correct data, but wrong answer.
	}
}
}

// Generate the checkboxses grouped by fieldsets.
foreach ($testData as $name => $values) {
$output .= '<fieldset><legend>'.htmlspecialchars ($name)."</legend></fieldset>\n";
foreach ($values as $val) {
	$output .= sprintf ($chbxTemplate, htmlspecialchars ($name), $val);
}
$output .= "</fieldset>\n";
}

 

As for your code, you do have some odd constructs in there. For instance $checkbox <= $checkbox will always be false, since a variable can't be anything but exactly the same as itself. You're also overwriting $towrite inside the loop, causing all but the last value to be discarded.

When it comes to saving stuff to a file, I recommend using file_put_contents () instead of the whole fopen (); fwrite (); fclose () routine.

 

Anyway, the above code should help you get on with your code. Just remember to be mindful of every little detail, and think about the why's and how's for every thing I've done. ;)

Should you need more help, just feel free to ask.

 

Thanks so much for the assistance. I'm looking at your code and I see the array up to a certain point. The amount of rows could be 500... Also my post above better asks my question, I've also included which data is being POSTed to the script.

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.