Forum

Thread tagged as: Question, Members

Add member tag on profile form submit

Can I use perch_member_add_tag when a member updates their profile?

I want to limit the amount of times members can change their name to once a month. So the idea is when they submit the profile form it adds a tag to the member with a one month expiry, and I check for that tag before outputting the form in the first place.

Stephen Turvey

Stephen Turvey 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

To handle the form submission specifically, you'd need to register a new app with your code and post the form to both Members and your app.

I had a quick look at the API section but not sure I have the time to get my head around making an app. Would it not work to simply point the redirect for the form to a PHP page which includes the add_tag line and then redirects back to the original page?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, you could do that if you're happy to test that the correct form has been submitted yourself.

I've attempted this but for some reason it won't recognise the isset name_change (works ok if I remove that).

profile.html

<perch:form id="profile" class="" method="post" app="perch_members" next="/account/update-name.php">

    <div class="mb-30">
        <perch:label for="first_name">First name</perch:label>
        <perch:input type="text" id="first_name" class="input" required="true" label="First name" />
        <perch:error for="first_name" type="required"><p class="alert alert--fail">Please add your first name</p></perch:error>
    </div>

    <div class="mb-30">
        <perch:label for="last_name">Last name</perch:label>
        <perch:input type="text" id="last_name" class="input" required="true" label="Last name" />
        <perch:error for="last_name" type="required"><p class="alert alert--fail">Please add your surname</p></perch:error>
    </div>

    <div>
        <perch:input type="hidden" id="name_change" value="true" />
        <perch:input type="submit" class="button button--success" value="Update name" />
        <perch:input type="hidden" id="token" />
    </div>

    <perch:success>
        <div class="alert"><p>Your details have been updated. <a href="/dashboard">Back to Dashboard</a></p></div>
    </perch:success>

</perch:form>

update-name.php

<?php
include('../perch/runtime.php');

if (isset($_POST['name_change']) && perch_member_logged_in()) {

    perch_member_add_tag('name-changed');

    PerchSystem::redirect('index.php?change=success');

}

else {
    PerchSystem::redirect('index.php?change=fail');
}

?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Perch manages the forms for you, so there won't actually be a field called name_change.

Ah. So how can I check the form was submitted before confirming the tag addition?

Stephen, in the success tag use perch_layout and add your perch_member_add_tag() in the layout.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can specify a custom prefix on the form tag - that should give you predictable field names.

I added prefix="name" to the profile form.

The hidden field now outputs as:

<input id="name_change" name="change" value="true" type="hidden">

I have tried both the following in my script and neither works:

if (isset($_POST['change']) && perch_member_logged_in()) {...
if (isset($_POST['name_change']) && perch_member_logged_in()) {...
Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you looked to see what you actually have in $_POST?

print_r($_POST);

It gives an empty array

Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you have any server side redirects that might be dropping the POST?

No, I don't think so.

Drew McLellan

Drew McLellan 2638 points
Perch Support

It's worth checking. Some people use a redirect to e.g. strip the .php file extension off the URL.

Double checked and no redirects in place. Cleared my .htaccess and tried again, still no joy.

Is there anything else that could be dropping POST?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Is this the page /account/update-name.php ? If so, you're not posting to it. You're posting back to your first page and then redirecting with the next attribute.

That's it - I just had to change next to action. Thank you.