Submit redirect only works logged in

Hi lovely Kirby Community,

I have a strange problem which I don’t understand at all:

I managed to “build” an evaluation tool for picture stuff with html form.

If the “submit” button is clicked, the user gets redirected to the next subpage.

It all works perfectly if a user is logged in, but the redirection doesn’t work without a user logged in. I can’t find the issue, maybe someone know this problem or can help me?

The template of the site looks like this:

<?php snippet('header') ?>

<?php $collection = $site->find('methode1')->children()->sortBy('title');?>

<?php $zahl = $collection->count() ;
$zahlindex =$collection->index();
$zahlindex2 =$zahlindex->indexOf($page);

$pageslisted= $site->pages()->listed();

?>

<main>

  <?php

  $error = '';
  $titel = $page->title();

  $intervall1 = $site->intervall1();
  $intervall2 = $site->intervall2();
  $intervall3 = $site->intervall3();
  $intervall4 = $site->intervall4();
  $intervall5 = $site->intervall5();

  $kriterium0 = $site->kriterium0();
  $kriterium1 = $site->kriterium1();
  $kriterium2 = $site->kriterium2();
  $kriterium3 = $site->kriterium3();

  $k0v = '';
  $k1v = '';
  $k2v = '';
  $k3v = '';

  ?>

  <section class="test">
    <!-- Artefakt-->
    <div class="artefakt">

            <?php if($page->hasFiles()) :?>
            <img src="<?= $page->image()->url() ?>" class="testobjekt">
            <?php endif?>

        </div>

        <!-- Bewertung-->

        <form method="post">

        <!-- Kriterium0-->

        <div class="form-group">
                <p class="krit"> <strong class="zahlstrong" >1.</strong><?= $kriterium0 ?> </p>
                <div class="interaction">
                    <input type="range" name="k0" id="k0" min="1" max="5" class="form-control range0"
                        value="<?php echo $k0v; ?>">
                    <div class="ticks">
                        <div></div>
                        <div></div>
                        <div></div>
                        <div></div>
                    </div>

                </div>

                <!-- Intervalle-->

                <div  class="legende" id="testedich">
                    <p id="p01" class="rangebtn range0" value="1"> <?= $intervall1 ?></p>
                    <p id="p02" class="rangebtn range0" value="2"> <?= $intervall2 ?></p>
                    <p id="p03" class="rangebtn range0" value="3"> <?= $intervall3 ?></p>
                    <p id="p04" class="rangebtn range0" value="4"> <?= $intervall4 ?></p>
                    <p id="p05" class="rangebtn range0" value="5"> <?= $intervall5 ?></p>
                </div>

            <!-- Kriterium1-->

            <div class="form-group">
                <p class="krit"> <strong class="zahlstrong">2.</strong><?= $kriterium1 ?> </p>
                <div class="interaction">
                    <input type="range" name="k1" id="k1" min="1" max="5" class="form-control range1"
                        value="<?php echo $k1v; ?>">
                    <div class="ticks">
                        <div></div>
                        <div></div>
                        <div></div>
                        <div></div>
                    </div>

                </div>

                <!-- Intervalle-->

                <div  class="legende" id="testedich">
                    <p id="p11" class="rangebtn range1" value="1"> <?= $intervall1 ?></p>
                    <p id="p12" class="rangebtn range1" value="2"> <?= $intervall2 ?></p>
                    <p id="p13" class="rangebtn range1" value="3"> <?= $intervall3 ?></p>
                    <p id="p14" class="rangebtn range1" value="4"> <?= $intervall4 ?></p>
                    <p id="p15" class="rangebtn range1" value="5"> <?= $intervall5 ?></p>
                </div>

                <!-- Kriterium2-->

                <div class="form-group">
                    <p class="krit"> <strong class="zahlstrong">3.</strong><?= $kriterium2 ?> </p>
                    <div class="interaction">
                        <input type="range" name="k2" id="k2" min="1" max="5" class="form-control range2"
                            value="<?php echo $k2v; ?>">
                        <div class="ticks">
                            <div></div>
                            <div></div>
                            <div></div>
                            <div></div>
                        </div>
                    </div>

                    <!-- Intervalle-->

                    <div  class="legende" id="testedich">
                        <p id="p21" class="rangebtn range2" value="1"> <?= $intervall1 ?></p>
                        <p id="p22" class="rangebtn range2" value="2"> <?= $intervall2 ?></p>
                        <p id="p23" class="rangebtn range2" value="3"> <?= $intervall3 ?></p>
                        <p id="p24" class="rangebtn range2" value="4"> <?= $intervall4 ?></p>
                        <p id="p25" class="rangebtn range2" value="5"> <?= $intervall5 ?></p>
                    </div>

                    <!-- Kriterium3-->

                    <div class="form-group">
                        <p class="krit"><strong class="zahlstrong">4.</strong> <?= $kriterium3 ?> </p>
                        <div class="interaction">
                            <input type="range" name="k3" id="k3" min="1" max="5" class="form-control range3"
                                value="<?php echo $k3v; ?>">
                            <div class="ticks">
                                <div></div>
                                <div></div>
                                <div></div>
                                <div></div>
                            </div>
                        </div>

                        <!-- Intervalle-->

                        <div  class="legende" id="testedich">
                            <p id="p31" class="rangebtn range3" value="1"> <?= $intervall1 ?></p>
                            <p id="p32" class="rangebtn range3" value="2"> <?= $intervall2 ?></p>
                            <p id="p33" class="rangebtn range3" value="3"> <?= $intervall3 ?></p>
                            <p id="p34" class="rangebtn range3" value="4"> <?= $intervall4 ?></p>
                            <p id="p35" class="rangebtn range3" value="5"> <?= $intervall5 ?></p>
                        </div>

                        <div class="form-group submit">
                            <input type="submit" name="submit" class="btn btn-info" list="tickmarks" value="Bewerten" />
                            </div>
                        </div>
        </form>

            </section>

</main>



<!-- Footer -->

<footer class="footer" id="footer">
    <div id="orientationprocess"></div>
    <h1 class="pagination">  Artefakt <?= $zahlindex2 + 1?>/<?= $zahl?> </h1>
    
    <?php if ($next= $page->getNext($pageslisted)) : ?>
<h1 class="next"><a href="<?= $site->find('auswertung4')->url()?>">weiter</a></h2>
<?php endif ?>

</footer>

</body>

</html>

<script>

var headertag = document.getElementById("header");
var headerheight = headertag.offsetHeight;
var mainTag = document.querySelector("main");

mainTag.style.paddingTop = headerheight+".px";

</script>

<?= js('assets/js/blurtest.js') ?>
<?= js('assets/js/rangebutton.js') ?>

<script>

var processtag = document.getElementById("orientationprocess");
var statustag = <?php echo $zahlindex2+1 ?>;
var gesamttag = <?php echo $zahl ?>;

widthy =(statustag/gesamttag)*100;
processtag.style.width = widthy +"%";

</script>

<?php

/** Datenübertragung */
echo $error;
$error = '';


function clean_text($string)
{
  $string = trim($string);
  $string = stripslashes($string);
  $string = htmlspecialchars($string);
  return $string;
}

  date_default_timezone_set('Europe/Berlin');
  $datum = date('d.m.Y, H:i', time());
  $datumuhrzeit = $datum ." Uhr";
  
  $session = $kirby->session();
  
    if (isset($_POST["submit"])) :
      $k0v = clean_text($_POST["k0"]);
      $k1v = clean_text($_POST["k1"]);
      $k2v = clean_text($_POST["k2"]);
      $k3v = clean_text($_POST["k3"]);

      $form_data = array(
        'datum'  => $datumuhrzeit,
        'Kriterium0'  => $k0v,
        'Kriterium1'  => $k1v,
        'Kriterium2'  => $k2v,
        'Kriterium3'  => $k3v,
        'info'  => 'Info',

      );
    
      $field = 'Auswertung';
      $data =  $form_data;

      $fieldData = page($page)->$field()->yaml();
      $fieldData[] = $data;
      $fieldData = yaml::encode($fieldData);
      try {
        page($page)->update(array($field => $fieldData));
      } catch (Exception $e) {
        return $e->getMessage();
      }

      if ($next= $page->getNext($collection)): 
        go($next->url());
        else : 
       go($site->find('auswertung4')->url()); ?>
   
<?php endif ?>
<?php endif ?>

The template itself doesn’t tell me that much, could you please post the code that handles the form submission?

I couldn’t make it work in the controller, so it’s in the end of my template:

<?php

/** Datenübertragung */
echo $error;
$error = '';


function clean_text($string)
{
  $string = trim($string);
  $string = stripslashes($string);
  $string = htmlspecialchars($string);
  return $string;
}

  date_default_timezone_set('Europe/Berlin');
  $datum = date('d.m.Y, H:i', time());
  $datumuhrzeit = $datum ." Uhr";
  
  $session = $kirby->session();
  
    if (isset($_POST["submit"])) :
      $k0v = clean_text($_POST["k0"]);
      $k1v = clean_text($_POST["k1"]);
      $k2v = clean_text($_POST["k2"]);
      $k3v = clean_text($_POST["k3"]);

      $form_data = array(
        'datum'  => $datumuhrzeit,
        'Kriterium0'  => $k0v,
        'Kriterium1'  => $k1v,
        'Kriterium2'  => $k2v,
        'Kriterium3'  => $k3v,
        'info'  => 'Info',

      );
    
      $field = 'Auswertung';
      $data =  $form_data;

      $fieldData = page($page)->$field()->yaml();
      $fieldData[] = $data;
      $fieldData = yaml::encode($fieldData);
      try {
        page($page)->update(array($field => $fieldData));
      } catch (Exception $e) {
        return $e->getMessage();
      }

      if ($next= $page->getNext($collection)): 
        go($next->url());
        else : 
       go($site->find('auswertung4')->url()); ?>
   
<?php endif ?>
<?php endif ?>

You are doing a page update which requires authentication, i.e. either a logged in user or $kirby->impersonate().

1 Like

Worked perfectly, thank you very much!