Import $_POST data from input checkboxes

22 views
Skip to first unread message

Lyndon Froese

unread,
Feb 26, 2024, 4:50:28 PMFeb 26
to redbeanphp
I love how I can just use

$book->import( $_POST );

to import the POST data generated by my HTML form.

Only problem is that it doesn't seem to work if I have checkboxes in my form that are for a many-to-many relationship.

Ex:

<input name="title" />
<input type="checkbox" name="category[]" value="1" />
<input type="checkbox" name="category[]" value="2" />
<input type="checkbox" name="category[]" value="3" />
<input type="checkbox" name="category[]" value="4" />

On the server side, if I receive the data like this:

$book->import( $_POST );

I get an error.

What I'm hoping for is a category_book table to be populated.

Is there a way to do this with import()?

Or do I have to do it like this:

$book = R::dispense('book');

$book->title = $_POST['title']

foreach($_POST['category'] as $category_id) {
  $category = R::dispense('category');
  $category->id = $category_id;
  $book->sharedCategoryList[] = $category;
}

R::store( $book );


Reply all
Reply to author
Forward
0 new messages