Skip to content

Instantly share code, notes, and snippets.

@somatonic
Last active September 24, 2021 12:19
Show Gist options
  • Save somatonic/5391391 to your computer and use it in GitHub Desktop.
Save somatonic/5391391 to your computer and use it in GitHub Desktop.
example repeater creation and save front-end form
<?php
$mypage = $pages->get("/about/");
if($input->post->submit){
$n = 1;
$title = "element_title_$n";
$url = "external_url_$n";
$mypage->setOutputFormatting(false);
while($input->post->$title){
// on first round remove all repeater items, to keep things easy.
// teasers is the repeater field
if($n == 1) $mypage->teasers->removeAll();
// create new repeater item
$item = $mypage->teasers->getNewItem();
// populate any fields in the repeater you want
$item->element_title = $input->post->$title;
$item->external_url = $input->post->$url;
// save the repeater item (a page itself)
$item->save();
// add the repeater item to the page
$mypage->teasers->add($item);
// update counter and field names
$n++;
$title = "element_title_$n";
$url = "external_url_$n";
}
if($n > 1) {
// save the page
$mypage->save('teasers');
}
}
?>
<form name="test" method="post">
<?php $count = 1; foreach($mypage->teasers as $key => $t): ?>
<input type="text" name="element_title_<?php echo $key+1?>" value="<?php echo $t->element_title?>"/>
<input type="text" name="external_url_<?php echo $key+1?>" value="<?php echo $t->external_url?>"/><br/>
<?php $count++; endforeach; ?>
<input type="text" name="element_title_<?php echo $count; ?>" value=""/>
<input type="text" name="external_url_<?php echo $count; ?>" value=""/><br/>
<input type="text" name="element_title_<?php $count++; echo $count; ?>" value=""/>
<input type="text" name="external_url_<?php echo $count; ?>" value=""/><br/>
<input type="submit" name="submit" value="submit"/>
</form>
@astvis
Copy link

astvis commented Sep 24, 2021

bc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment