Basic POST form in loop, trying to update single Express Entry, updates all instead

I’m trying to work on a novel approach to a long-winded functional need I have, and the method I’m trying is doing “too much” than I want.

Example code I’m working with:

$testEntity = Express::getObjectByHandle('express_object_here');
$listTest = new Concrete\Core\Express\EntryList($testEntity);
$testResults = $listTest->getResults();


foreach($testResults as $testResult) {
    ?>
    <p><br>Start of debug
    <?php echo sprintf('Current User Selected: %s', $testResult->getUserSelectedAttribute());
    ?><p>Click on this button to modify entry
    <p><input type="submit" name="submit">
    <?php if(isset($_POST["submit"])) {
        $testResult->setAttribute("test_boolean_attribute", "True");
    }
    ?>
    <form method="post">
            <button name="second">Second Test</button>
    </form>
    <?php
    if(isset($_POST['second'])) {
        $testResult->setAttribute("test_boolean_attribute", "True");
    }

    ?>
    <?php
}

In this very rough PoC I’m trying to have it generate a button (I’m using two buttons just for method testing, second button works, first doesn’t) for each Entry in the Express Entity list. And the intent is to have the boolean attribute for JUST ONE Express Entry be set from False to True.

However, when I click on any of the “second test” buttons, it sets that Attribute to True for… ALL of the Entries. And again I just want it to set to True for that specific entry.

I’m not yet seeing what I’m doing wrong here. And I’m trying to build a self-serve interface here without $endUser using the dashboard interface, as I have a lot less presentation control over that and it doesn’t work for the UX I’m trying to build.

Anyone have any ideas what I can do about this? Thanks!

Okay solved my own problem, had to move the POST operation outside of the forloop:

$testEntity = Express::getObjectByHandle('express_object_here');
$listTest = new Concrete\Core\Express\EntryList($testEntity);
$testResults = $listTest->getResults();


foreach($testResults as $testResult) {
    ?>
    <p><br>Start of debug
    <?php echo sprintf('Current User Selected: %s', $testResult->getUserSelectedAttribute());
    ?><p>Click on this button to modify entry
    <p><input type="submit" name="submit">
    <?php if(isset($_POST["submit"])) {
        $testResult->setAttribute("test_boolean_attribute", "True");
    }
    ?>
    <form method="post">
            <button name="second">Second Test</button>
    </form>
    <?php


    ?>
    <?php
}

if(isset($_POST['second'])) {
    $testResult->setAttribute("test_boolean_attribute", "True");
}

Testing now shows that it only updates the entry the button is beside! YAY! Thanks me! XD

Okay it does update one entry, but it seems to update the “latest” entry, and I’m not sure what to do about that… since I want it to update the entry for that line… which of course in the majority of cases will not be the “latest” entry… :confused:

Could swear I had it doing the right one each time…

Beside mixing view and stuff that should go to some kind of controller.
You are taking wrong approach to that.
You should insert entry id in hidden input for each form.
Then on POST, you should get Express entry by id (not whole list and then looping it).
→ check if Express entry exist/if user has credentials (if necessary) etc. → update entry → possibly redirect.

Yeah I realise after reading what you wrote a few more times, and even seeing an example that I tried of that nature, that that seems to be the way to go.

I created the other thread (about getEntry) before I realised what you were proposing.

Thanks! :slight_smile: