1

i had a simple input field using propel to change values in a database. The error was displayed on the bottom of the form with what was wrong. Now i wan't the error to be displayed behind the correct input field. The problem with that is that the input fields are build dynamicly using a foreach loop. So when i wan't to show the error on the front-end it shows it after every input field with the same name. In this case i have 3 fields with 'afsluitkosten' if i have a error for changing only one i wan't it to show only after the matching input field.

if (isset($_POST['opslaan'])) {

    $naam = $_POST['naam'];
    $afsluitkosten = $_POST['afsluitkosten'];
    $percentage = $_POST['percentage'];
    $btw = $_POST['btw'];
    $id = $_POST['opslaan'];
    if ($afsluitkosten < 15)
     {
        if ($percentage < 1)
         {
            if ($btw < 50)
             {
                $annuleringOpslaan = AnnuleringsverzekeringQuery::create()->findOneById($id);
                $annuleringOpslaan->setNaam($naam);
                $annuleringOpslaan->setAfsluitkosten($afsluitkosten);
                $annuleringOpslaan->setPercentage($percentage);
                $annuleringOpslaan->setBtw($btw);
                $annuleringOpslaan->save();

                $opslaanSucces = "Annuleringsverzekering $naam is opgeslagen";
            } else {
                $fout1 = "De btw van annuleringsverzekering $naam moet onder de 50 zitten";
            }
        } else {
            $fout2 = "De percentage van annuleringsverzekering $naam moet onder de 1 zitten";
        }
    } else {
        $fout3 = "De afsluitkosten van annuleringsverzekering $naam moet onder de 15 zitten";
    }
};

// Gegevens ophalen en laten zien in front end
$annuleringen = AnnuleringsverzekeringQuery::create()
    ->find();

$annuleringarray = array();


foreach ($annuleringen as $annulering) {
    $array["id"] = $annulering->getId();
    $array["naam"] = $annulering->getNaam();
    $array["afsluitkosten"] = $annulering->getAfsluitkosten();
    $array["percentage"] = $annulering->getPercentage();
    $array["btw"] = $annulering->getBtw();
    $array["fout1"] = $fout1;
    $array["fout2"] = $fout2;
    $array["fout3"] = $fout3;

    array_push($annuleringarray, $array);
};

This is the html

    <h2>{$doctitle}</h2>

    {foreach $annuleringarray as $array}
    <form action="annuleringsverzekering_bewerken.php" method="post">

        <label for="naam">Naam</label><input name="naam" value="{$array.naam}"><br>
        <label for="afsluitkosten">Afsluitkosten</label><input name="afsluitkosten" value="{$array.afsluitkosten}"><span class="error">{$array.fout3}</span><br>
        <label for="percentage">Percentage</label><input name="percentage" value="{$array.percentage}"><span class="error">{$array.fout2}</span><br>
        <label for="btw">Btw</label><input name="btw" value="{$array.btw}"><span class="error">{$array.fout1}</span> <br> 
        <button name="opslaan" value={$array.id}>Opslaan</button> <br> <br>

    </form>
    {/foreach}

    <h3 style="color:green;">{$opslaanSucces}</h3>

</body> ```

1 Answer 1

1
$errors=array(); // on before `if (isset($_POST['opslaan'])) {`

And errors like following passing error to array:

$errors[] = "Annuleringsverzekering $naam is opgeslagen";

and displaying:

if ($errors){
 foreach($errors as $error){
 //do what ever you want here `echo $error;` or not echo $error;
 }
}

if you want to show errors below to input having error then check this example: https://www.tutorialrepublic.com/php-tutorial/php-mysql-crud-application.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.