如何在php中使文本框只读

如何在php中使文本框只读,php,codeigniter,Php,Codeigniter,如何在此代码中使文本框为只读 <?php echo form_input(array( 'name'=>'price', 'value'=>$item['price'], 'size'=>'6' )); ?> 我只希望某些用户只能读取该值,而不能更改它。请尝试类似操作 <?php echo form_input(array('name'=>'price','value'=>

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

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

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

请尝试类似操作

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

您可以通过以下方式完成:

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

这将与xhtml兼容


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

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

从CI表单字段:

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

将字段的id用作:

 $("#id_offield").attr("readonly",true);
在html中:

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

等等

希望这会有帮助

#试试纯PHP
# 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; ?>>
如果(isset($_POST['something'])){ #不要设置readonly属性 $readonly=''; }否则{ #设置只读属性 $readonly='readonly'; }
请注意,在表单验证中,您仍然需要确保内容没有被不允许的用户更改,因为设置html属性很容易被wiley撤销。required=“true”不是有效的HTML5语法,请检查此项,
# 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; ?>>