Jump to content

How to save field page type checkbox?


diegonella
 Share

Recommended Posts

This is my code

<form method="post" action="<?php echo $page->url;?>">
<?php
	$idiomas = $pages->get('/tabla/idioma/')->children();
	foreach($idiomas AS $idioma) {
?>
	<input type="checkbox" name="idiomas[]" id="idioma_<?php echo $idioma->id; ?>" value="<?php echo $idioma->id; ?>" <?php if ($page->htl_idiomas->has($idioma)) { echo "checked"; } ?>> <label for="idioma_<?php echo $idioma->id; ?>" class="label"><?php echo $idioma->title; ?></label>
<?php } ?>
<input type="submit" value="submit" />
</form>

What I could not do it when I get the POST, save the selected values

if($input->post->btn_submit) {
	$page->setOutputFormatting(false);
	foreach($input->post->htl_idiomas- AS $idioma_id ){
		$page->htl_idiomas = $pages->get((int)$idioma_id);
	}
	$page->save();
}

Can you give me a hand?

Thanks

Link to comment
Share on other sites

I haven't looked thoroughly through your code, but the one thing that stands out is that you are checking for posting of btn_submit and yet your submit button is not named as such.

Try this:

<input type="submit" value="submit" name="btn_submit" />
 

That should at least get your processing section to do its thing.

Link to comment
Share on other sites

Are you really wanting to do this:

$page->htl_idiomas = $pages->get((int)$idioma_id);
 

Don't you want to set htl_idiomas to the value of the checkbox from the post?

This is something you might find quite helpful:

http://processwire.com/talk/topic/59-module-want-form-builder/?p=11639

It shows you how to automatically generate a front-end form from an existing page's fields. This will probably take care of everything for you.

Link to comment
Share on other sites

I solved it well, right?

foreach ($page->htl_idiomas AS $k => $p ) {
	$page->htl_idiomas->remove($k);
}
$idiomas = array();
foreach($input->post->htl_idiomas AS $idioma_id ){
	$idiomas[] = $pages->get((int)$idioma_id);
}
$page->htl_idiomas->import($idiomas);
Link to comment
Share on other sites

That should work. You could also do it this way:

$page->htl_idiomas->removeAll();
foreach($input->post->htl_idiomas AS $idioma_id ) {
   $p = $pages->get((int) $idioma_id); 
   if($p->id && $p->viewable()) $page->htl_idiomas->add($p); 
} 
However, something that is missing that you definitely want to add is more validation. Meaning, check to make sure that the pages you are adding to itl_idiomas are in fact the pages you intend. This prevents the possibility of someone manipulating the POST values to include page IDs outside of those you allow. So I would do this instead:
$idiomas = $pages->get('/tabla/idioma/')->children();
$page->htl_idiomas->removeAll();
foreach($input->post->htl_idiomas AS $idioma_id ) {
   $p = $idiomas->find("id=" (int) $idioma_id)->first();
   if($p && $p->viewable()) $page->htl_idiomas->add($p); 
}
  • Like 2
Link to comment
Share on other sites

  • 9 years later...

I still can't make sense of this. It seems each post in this thread has a piece of the puzzle but I can't put it all together.

If you have a front end form for logged in users to edit checkbox selections how do you save it to the page? I've been at this for hours can someone put it all together?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...