Page 1 of 1

Save checkbox values in plugins

Posted: Sun Jan 06, 2019 4:05 pm
by battlethem
Hello!
In the plugin settings, whenever I add input type="text", I can save any values in an array just fine. I save the settings and the values stay there.
But checkboxes always reset because I can't seem to save checkbox values in arrays like text values.

Here's the array where I save the values for input fields.

Code: Select all

public function init() {
	$this->dbFields = array(
	'cars'	=> '',		//text
	'bikes'	=> ''		//checkbox
	);
}
And here's the input in the plugins settings. The 'cars' input saves, but I struggle with saving 'bikes' value. (probably because I never even stated the value for the checkbox)

Code: Select all

// Cars - text
$html .= '<div>';
$html .= '<input name="cars" type="text" class="form-control" value="'.$this->getValue('cars').'">';
$html .= '</div>'.PHP_EOL;

// Bikes - checkbox
$html .= '<div>';
$html .= '<input name="bikes" type="text" class="form-control" value="'.$this->getValue('bikes').'">';
$html .= '</div>'.PHP_EOL;
As it turned out, there are no examples of checkboxes anywhere in Bludit , and I couldn't find any plugins that would have them, so I've been struggling with it myself. What's the best way to make checkboxes work?
Thank you!

Re: Save checkbox values in plugins

Posted: Sun Jan 06, 2019 8:20 pm
by Edi
battlethem wrote: Sun Jan 06, 2019 4:05 pm And here's the input in the plugins settings. The 'cars' input saves, but I struggle with saving 'bikes' value. (probably because I never even stated the value for the checkbox)

Code: Select all

// Cars - text
$html .= '<div>';
$html .= '<input name="cars" type="text" class="form-control" value="'.$this->getValue('cars').'">';
$html .= '</div>'.PHP_EOL;

// Bikes - checkbox
$html .= '<div>';
$html .= '<input name="bikes" type="text" class="form-control" value="'.$this->getValue('bikes').'">';
$html .= '</div>'.PHP_EOL;
I'm not shure if I understand your question...

But your example uses in both cases a text field. A checkbox would be type="checkbox".

Re: Save checkbox values in plugins

Posted: Mon Jan 07, 2019 5:09 pm
by battlethem
My bad, that's a typo, it should look like this:

Code: Select all

// Cars - text
$html .= '<div>';
$html .= '<input name="cars" type="text" value="'.$this->getValue('cars').'">';
$html .= '</div>'.PHP_EOL;

// Bikes - checkbox
$html .= '<div>';
$html .= '<input name="bikes" type="checkbox" value="'.$this->getValue('bikes').'">';
$html .= '</div>'.PHP_EOL;
To make it clear: I'm able to save a string value in the plugin settings (so when I refresh the page it stays there), but the checkbox always resets. What is the right way to save boolean value in this situation?

Thanks for the reply btw

Re: Save checkbox values in plugins

Posted: Mon Jan 07, 2019 9:07 pm
by Edi
You can do it like this:

Code: Select all

$html .= '<div>';
$html .= '<input type="hidden" name="vehicle1" value="" />';
$html .= '<input type="checkbox" name="vehicle1" value="checked" '.$this->getValue('vehicle1').'>I have a bike<br>';
$html .= '<input type="hidden" name="vehicle2" value="" />';
$html .= '<input type="checkbox" name="vehicle2" value="checked" '.$this->getValue('vehicle2').'>I have a car<br>';
$html .= '<input type="hidden" name="vehicle3" value="" />';
$html .= '<input type="checkbox" name="vehicle3" value="checked" '.$this->getValue('vehicle3').'>I have a boat<br>';
$html .= '</div>';
The idea is from here:

http://dustinbolton.com/submit-unchecke ... box-value/