Jump to content

save selected checkboxes on page submit through api


webhoes
 Share

Recommended Posts

A user needs to use the datepicker to fill in a date and check 2 checkboxes. On submit a new page will be generated. Everything works except getting wich checkboxes are checked and also set them checked on the page.

How do I solve this?

 

<?php

if (isset($_POST['submit_mating'])) {
    $date = $input->post->mating;
    $date = $sanitizer->date($date);
    $title = date("Y-m-d-g-i", $date);

    $p = new Page();
    $p->setOutputFormatting(false);
    $p->template = 'feeding'; // example template
    $p->parent = 1089; //$user->id; // example parent
    $p->name = $user->name . "-mating-" . $date; // example name
    $p->title = date("dd-mm-YYYY-g-i",$date);

    $matingTurtles = new PageArray();
    foreach ($turtles as $check) {

        if($input->post->mating == $selected) $matingTurtles->add($check->id);//how to check if the box is checked?
    }
    $p->turtles_mating = $matingTurtles;

    $p->save();

    $p->title->setLanguageValue('default', $title); //default
    $p->title->setLanguageValue('de', $title); //german
    $p->title->setLanguageValue('nl', $title); //dutch

    //echo var_dump($p);
    $p->save();

    $session->redirect($page->url);
} else {
    echo "Click the datepicker to set the date of mating";

    $options = '';
    foreach ($turtles as $mating){
        $selected = $mating->id == $input->whitelist->mating ? " selected='selected' " : '';
        $options .= '<label><input$selected class="uk-checkbox" name="mating[]" type="checkbox" id="' . $mating->id . '">' . $mating->title . '</label>';
    }

    echo '<form role="form" method="post" action="" >
      <div class="form-group">     
        <div class="InputfieldContent uk-form-controls">
        <input id="mating" class="uk-input uk-form-width-medium uk-datepicker" name="mating" type="text" maxlength="2048" size="25" data-uk-datepicker="{format:\'DD-MM-YYYY\'}"></div>          
      </div>
      <div class="uk-fieldset">
       <div class="uk-margin uk-grid-small uk-child-width-auto uk-grid">
      ' . $options . '</div></div>
      <button type="submit" name="submit_mating" class="btn btn-default">Submit</button>
    </form>';
}


?>
Link to comment
Share on other sites

Hi,

you can check if the checkbox is checked and check it in your page like this:

// sanitize as text first!
if($input->your_checkbox_name == "on"){
	//checkbox cheked
	$page->your_checkbox = 1; //if checkbox not set value is 0
}

 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...