Save checkbox values in plugins

Post Reply
User avatar
battlethem
Jr. Bludit
Posts: 4
Joined: Sun Dec 09, 2018 7:52 pm

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!
User avatar
Edi
Site Admin
Posts: 3121
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

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".
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
User avatar
battlethem
Jr. Bludit
Posts: 4
Joined: Sun Dec 09, 2018 7:52 pm

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
User avatar
Edi
Site Admin
Posts: 3121
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

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/
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
Post Reply