Jump to content

form input read only for multiple fields


micksulley
Go to solution Solved by ginerjm,

Recommended Posts

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

 

Link to comment
Share on other sites

  • Solution

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 by ginerjm
  • Like 1
Link to comment
Share on other sites

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

  • Thanks 1
Link to comment
Share on other sites

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?

  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.