2

I'm using a CMS and a form builder extension, the extension allows me to run PHP scripts when on page load.

I'm also able to add custom 'Additional Attributes' to individual elements, the 'Additional Attributes' section is a free text field allowing me to enter custom text e.g. maxlength="20", placeholder="Admin Only" you can see below.

I have a form that contains the following elements (showing only four for brevity, actual form contains many more);

// user fields
<input type="text" value="" name="form[Name]" id="Name" class="form-control" maxlength="20">
<input type="text" value="" name="form[Address]" id="Address" class="form-control" maxlength="20">
// admin only fields
<input type="text" value="" name="form[AdminName]" id="AdminName" class="form-control" maxlength="20" placeholder="Admin Only">
<input type="text" value="" name="form[AdminComment]" id="AdminComment" class="form-control" maxlength="50" placeholder="Admin Only">

Using only PHP (and not a DOM Parser) I need to be able to add the readonly attribute all form elements that are for admins only. In the form above that is the last two inputs.

<?php
    if (userid == something) {
        // add 'readonly' attribute to all admin inputs
    } 
?>

I was thinking of using str_replace or the form 'Additional Attributes' feature somehow?

I can't use Javascript / JQuery.

7
  • Use a PHP DOM parser Commented Apr 16, 2020 at 14:21
  • Why is that not an option? It is using PHP. Commented Apr 16, 2020 at 14:34
  • 1
    You've been here long enough and have enough reputation to know that we typically just don't write up an answer as a "try". Show us how you tried to solve the problem yourself, then show us exactly what the result was, and tell us why you feel it didn't work. Give us a clear explanation of what isn't working and provide a Minimal, Complete, and Verifiable example. Commented Apr 16, 2020 at 14:51
  • 1
    @JayBlanchard It's fine if you don't know the answer, but i'd prefer to leave the questions up for a while, perhaps someone can help me. I feel that I've provided enough information in my question and demonstrated effort, if not then I'm sure the question will be closed. Thanks. Commented Apr 16, 2020 at 14:58
  • 1
    Demonstrated effort? There is no code demonstrating effort in this question at all. Look, I am trying to help you out, but you have to help me (and others) help you. Do you want me to show you how to do a string replace? Commented Apr 16, 2020 at 15:07

1 Answer 1

1

The solution I found was to add a custom attribute to the html element e.g. xyz, then use str_replace in order to replace this attribute with readonly.

The form inputs weer generated from a PHP variable $formData.

<input type="text" value="" name="form[Name]" id="Name" class="form-control" maxlength="20" xyz>

<?php
    if (userid == something) {
        str_replace("xyz","readonly",$formData)
    } 
?>
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.