Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting READONLY value conditionally

Status
Not open for further replies.

RPGguy

Programmer
Jul 27, 2001
73
US
When populating my customer entry form I would like the Customer # field set to read only only if I'm not in 'ADD' mode. I use this code to set what script is to call based on my input variable:
if ($key == "**add")
{
$type="addCust.html";
}
else
{
$type="updCust.html";

As part of the 'else' code I would like to set this 'cust' field:
<input type=&quot;text&quot; name=&quot;cust&quot; value=&quot;<? echo $line2[Cust]; ?>&quot; size=&quot;33&quot; tabindex=&quot;1&quot; ></td>

to readonly=true. It's probably simple but I can't find the correct syntax.

Thanks in advance.

Scott
 
Set this serverside with PHP. you have set a value called add in the $key variable

Code:
echo &quot;<input type=\&quot;text\&quot; name=\&quot;cust\&quot; value=\&quot;&quot;.$line2[Cust].&quot;\&quot; size=\&quot;33\&quot; tabindex=\&quot;1\&quot;&quot;;

if (!$key=='**add&quot;){
  echo &quot; readonly &quot;;
}
echo &quot;></td>&quot;;


Bastien

Any one have a techie job in Toronto, I need to work...being laid off sucks!
 
And solving it client-side...

First, include the
Code:
disabled
phrase in the input element...

Code:
<input type=&quot;text&quot; disabled=&quot;true&quot; name=&quot;test&quot;>

Now, using Javascript you can enable it...

Code:
document.myForm.test.disabled = false;

And you can disable it again...

Code:
document.myForm.test.disabled = true;

Whilst I don't have access to a suite of test browsers here, it works fine using IE6 in Windows. I'm confident you will get good milage from this on the new browsers.

Jeff
 
BabyJeffy,

I'm pretty sure that setting a text field to readonly and disabling it are 2 completely different things. I had a problem a while back in one of my projects where I disabled a field and then tried to access it's value. Since the field was disabled I pulled a null value instead of what was in the field. Readonly fixed this problem as it allowed me to pull the value from the field.


-kaht

banghead.gif
 
kaht...

I verified the code snippet, and I have no problems accessing the value property of the input element (whether it's disabled or not) using IE6, Opera7.21 or NN7.1 (in windows).

I can imagine that there will be some earlier browser versions that this doesn't work for... but fortunately it seems to work on all the current versions.

To be honest, I've never used this technique in a production web solution... there are some many things you can do - and so little time to code them it appears :)

Great for an intranet where you know the browser spec in advance, though!

Jeff
 
Actually.... I think I remember what the problem was. When the form was submitted all the disabled fields were not submitted. Sorry for the confusion.

-kaht

banghead.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top