如何在 php 中使文本框只读

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15524602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:25:07  来源:igfitidea点击:

How can I make a textbox readonly in php

phpcodeigniter

提问by user1796164

How can I make a textbox readonly in this code

如何在此代码中只读文本框

<?php 
    echo form_input(array(
        'name'=>'price',
        'value'=>$item['price'],
        'size'=>'6'
    ));
?>

I want only certain users to only read the value and not be able to change it.

我只希望某些用户只能读取值而不能更改它。

回答by Gautam3164

Try like

试试像

<?php 
  echo form_input(array('name'=>'price','value'=>$item['price'],'size'=>'6',
     'readonly'=>'true'));
      //Or 'readonly'=>'readonly'
?>

回答by Muhammad Raheel

$options    =   array(
                    'name'      =>  'price',
                    'value'     =>  $item['price'],
                    'size'      =>  '6'
);

if(!$allowed_user){
    $options['readonly']    =   'readonly'
}

echo form_input($options);

回答by Skatox

You can do it with:

你可以这样做:

<?php 
  echo form_input(array('name'=>'price','value'=>$item['price'],'size'=>'6',
     'readonly'=>'readonly')); 
?>

This will be xhtml compatible.

这将与 xhtml 兼容。

Just be sure to NOTread it on server side, don't trust on client data, because HTML can be changed to allow to modify values.

请确保不要在服务器端读取它,不要相信客户端数据,因为可以更改 HTML 以允许修改值。

回答by Faris Rayhan

# Try pure PHP
if(isset($_POST['something'])){
# Do not set the readonly attribute
$readonly ='';
}else{
# Set the readonly attribute
$readonly = 'readonly';
}
<input type="text" <?php echo $readonly; ?>>

回答by sandip

There are many ways to do this:

有很多方法可以做到这一点:

from CI form field:

来自 CI 表单字段:

  echo form_input(array('name'=>'price','value'=>$item['price'],'size'=>'6','readonly'=>'true'));

from jquery/javascript:

来自 jquery/javascript:

using id of the field as:

使用字段的 id 作为:

 $("#id_offield").attr("readonly",true);

in html:

在 html 中:

<textarea rows="4" cols="50" readonly>

etc!

等等!

hope it will help!

希望它会有所帮助!