micksulley Posted December 11, 2021 Share Posted December 11, 2021 I have a form which displays values and allow changes. The "id" field is set to be read only, but I want to set a couple of other fields to read only as well. How can I do that? This is the section of code <form method="post"> <?php foreach ($assetx as $key => $value) : ?> <label for="<?php echo $key; ?>"><?php echo ucfirst($key); ?> <input type="text" name="<?php echo $key; ?>" id="<?php echo $key; ?>" value="<?php echo escape($value); ?>" <?php echo ($key === 'id' ? 'readonly' : null); ?>> </label> <?php endforeach; ?> <input type="submit" name="submit" value="Submit"> </form> Thanks Mick Quote Link to comment Share on other sites More sharing options...
Solution ginerjm Posted December 11, 2021 Solution Share Posted December 11, 2021 (edited) A little cleaned up version of your code and my notes on it. echo "<form method='POST'>"; foreach ($assetx as $key => $value) { echo "<label>" . ucfirst($key) . " "; $ro = ($key === 'id') ? 'readonly' : ''; echo "<input type='text' name='$key' id='$key' value='$value' $ro>" echo '</label><br>'; } echo "<input type='submit' name='submit' value='Submit'>"; echo '</form>'; 1. One doesn't have to keep going into and out of php mode. It especially makes it easier to comprehend if one doesn't. 2. I can't find any mention of an 'escape' function in my php manual. Can you explain what you are trying to do with your use of it? 3 Your foreach is processing an array I assume. If that is so, how do you expect to have 2 keys having the same value of 'id' ? Edited December 11, 2021 by ginerjm 1 Quote Link to comment Share on other sites More sharing options...
Barand Posted December 11, 2021 Share Posted December 11, 2021 Create an array of those field names which are to be read only, for example $readonly = ['id', 'username', 'email']; then $ro = (in_array($key, $readonly)) ? 'readonly' : ''; 1 1 Quote Link to comment Share on other sites More sharing options...
micksulley Posted December 11, 2021 Author Share Posted December 11, 2021 Thank you both for your replies, I now have it working. I have a blank line separating each data line, but that is no big deal. Re your comments ginerjm, I have followed several on-line tutorials to create my code and I must admit I don't understand all of it. I have searched for a good tutorial and not found one which starts from basics and actually explains how it all works, which results in me copying code, using it and hoping for the best. Some of the tutorials conflict with other, which is not helpful. The multiple php calls was from a tutorial which maintained that it was clearer that way, but on balance I think I agree with you. Thanks for your help Mick 1 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 11, 2021 Share Posted December 11, 2021 Hopefully you understand what I wrote for you as well as Barand's finish with the extra array to help identify those items to be R/O. As for php mode switching - I don't know who would think that doing things that way is 'clearer'. If anything it is more clumsy and adds so much to the coding as to make it tedious to read as well as to maintain later on. As for the 'extra line' complaint. Well - that means you need to stop with the loop to build the form and actually build it manually so that you can place the fields where you want them. Learn the use of css and div tags as well as margins, padding and perhaps html tables despite the current downplaying of that style of presentation. Having fun? 1 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 11, 2021 Share Posted December 11, 2021 Should have asked - do you understand the code that I wrote for you fully? And the part that Barand added? You should have a shortcut to the PHP manual function reference so that you can quickly look something up and truly understand it. Here is what I use: https://www.php.net/manual/en/funcref.php Add a browser icon on your desktop with a shortcut key assigned so you can call it up any time you need help. It has a handy search box (use Alt-S) in the upper right corner so you can type in a name to find the explanation for anything. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.