8

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.

zessx
  • 68,042
  • 28
  • 135
  • 158
user1796164
  • 131
  • 1
  • 4
  • 14
  • 1
    Be aware that in your form validation you'll still need to make sure that the content hasn't been changed by a disallowed user, as setting a html attribute can easily be undone by someone wiley. – Ivo Mar 20 '13 at 13:24

5 Answers5

15

Try like

<?php 
  echo form_input(array('name'=>'price','value'=>$item['price'],'size'=>'6',
     'readonly'=>'true'));
      //Or 'readonly'=>'readonly'
?>
GautamD31
  • 28,552
  • 10
  • 64
  • 85
  • required="true" is not a valid HTML5 syntax, check this out, https://stackoverflow.com/questions/3004703/required-attribute-html5#3012975 – Pizzicato Oct 19 '17 at 09:42
3
$options    =   array(
                    'name'      =>  'price',
                    'value'     =>  $item['price'],
                    'size'      =>  '6'
);

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

echo form_input($options);
Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103
3
# 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; ?>>
Faris Rayhan
  • 4,500
  • 1
  • 22
  • 19
2

You can do it with:

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

This will be xhtml compatible.

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

Skatox
  • 4,237
  • 12
  • 42
  • 47
0

There are many ways to do this:

from CI form field:

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

from jquery/javascript:

using id of the field as:

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

in html:

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

etc!

hope it will help!

sandip
  • 3,279
  • 5
  • 31
  • 54