Jump to content

Multi checkbox back to db


davo
 Share

Recommended Posts

I have an html form on my page which displays a list of check boxes which a user would check multiple times to select the appropriate categories; it looks like this:


<?php
echo "Type of Sale :";
    $type = $pages->find("template='Sale_Type'");
    foreach($type as $t)
         echo "<input type='checkbox' name='type' value='{$t->id}'>{$t->title}<br>";

?>

Now that's great, it displays the correct list of options.

The next bit is where I become unstuck. The page will submit this back to itself for submission into the db as a new page along with other fields. The problem is, it only submits into the db the last checkbox selected. I'm attempting it like this:

$message = "Excellent news. Your garage sale has been listed.";
if($input->post->submit){
    $title = $sanitizer->text($input->post->title);
    $address1 = $sanitizer->text($input->post->address1);
  $address2 = $sanitizer->text($input->post->address2);
 $postcode = $sanitizer->text($input->post->postcode);
 $telephone = $sanitizer->text($input->post->telephone);
 $date = $sanitizer->text($input->post->date) . "12:00:00";
 $town = $sanitizer->text($input->post->town);
 $type = $sanitizer->text($input->post->type);

echo $type;

   $p = new Page();
    $p->template = $templates->get("listing");
    $p->parent = $pages->get(1016);
    $p->title = $title;
    $p->address1 = $address1;
    $p->address2 = $address2;
    $p->postcode = $postcode;
    $p->telephone = $telephone;
    $p->town_select = $town;
    $p->Date = $date;
    $p->sale_type_select = $type;
    $p->save();
 }
}

You'll see I've echo'd into there the $type variable to see what it hold, and as expected, it only ever holds the last checkbox that was ticked. Processwire does allow the field to hold multiple pages as I can enter multiple checkboxes from the backend.

So my question is, how do i get my form to post multiple values back, and then how to get it to enter them into the database?

(my php skill is fairly low and I'm very new to processwire; this is a sort of learning project for me)

Link to comment
Share on other sites

Thank you both. Both answers solved my issue.

I resolved it like this:


foreach($input->post->type as $t)
             $p->sale_type_select =  $t;

Having read a number of other posts I'm trying to keep in mind validation, could I potentially run the post through the sanitizer function?

  • Like 1
Link to comment
Share on other sites

Never trust user input. So yes, you should sanitize user submitted values. :)

I did have a go at it but it caused a code 500 error. Would you mind showing me how I would change mind code to include this please. I have used the sanitizer function on single variables but not an array.

Link to comment
Share on other sites

Since your values appear to be integers, you can sanitize them as easily as:

$sanitizedValue = (int) $t; 

That sanitizes, but doesn't validate the value. Validation is also important, especially with something like an ID number that indicates a reference to something else. This is even more important in ProcessWire, where page IDs can also represent things like users, admin pages and more. To validate, you'd want to make sure the number is valid for the selection. To do this, you'd need to confirm that the value you were given was present in the list of selectable options you gave to the user. Or, if you know the selection is limited to pages from a certain parent or template, you could validate like this:

$p = $pages->get($sanitizedValue); 
if($p->id && $p->template == 'your-valid-template' && $p->parent->id == 123 && $p->viewable()) {
  // it is valid
} else {
  // it is not valid
}
  • Like 5
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...