Home » Php » codeigniter – How can I make a textbox readonly in php

codeigniter – How can I make a textbox readonly in php

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

How to&Answers:

Try like

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

Answer:

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

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

echo form_input($options);

Answer:

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.

Answer:

# 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; ?>>

Answer:

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!