how to display session data in a view CI - codeigniter

I tried store data in a session using CI in my register function.
public function register(){
$firstname = $this->input->post('firstname');
$email= $this->input->post('email');
$dev_info = array('fname'=>$firstname, 'eaddress'=>$email);
$this->session->set_userdata($dev_info);
}
and in my other function verification I want to get the session data and pass to the view
public function verification(){
$data['fname'] = $this->session->userdata('fname');
$data['eaddress'] = $this->session->userdata('eaddress');
$this->load->view('index', $data);
}
and I want to save its value in input type
in the view
<input type="hidden" id="firstname" value="<?php echo $fname?>">
<input type="hidden" id="email" value="<?php echo $eaddress?>">
but i am having a hard time saving its value in an input everytime I check using view source. Please help!

you can get session variables one by one also
$first_name = $this->session->userdata('fname');
$email_address = $this->session->userdata('eaddress');
in view you can use these variables or you can write code for view like this
<input type="hidden" id="firstname" value="<?php echo $this->session->userdata('eaddress'); ?>">
<input type="hidden" id="email" value="<?php echo $this->session->userdata('fname')?>">

in controller create session for ur input data
$data['fname'] = $this->session->userdata('fname');
$data['eaddress'] = $this->session->userdata('eaddress');
in view retrieve the input data from the session by adding the value by set_value
<input type="hidden" id="firstname" value="<?php echo set_value('fname'); ?>">
<input type="hidden" id="email" value="<?php echo set_value('eaddress'); ?>">

Related

Message: Trying to get property 'firstname' of non-object Codeigniter

view
<form action="/user/edit" method="POST">
<?php $fuser = $this->session->userdata('fname');?> <input name="firstname" class="input" type="text" value="<?php echo $fuser->firstname;?>" placeholder="Enter First Name"><br><br>
<?php $luser = $this->session->userdata('lname');?> <input name="lastname" class="input" type="text" value="<?php echo $luser->lastname;?>" placeholder="Enter Last Name"><br><br>
<?php $grd = $this->session->userdata('grade');?> <input name="grade" class="input" type="text" value="<?php echo $grd->grade;?>" placeholder="Enter Grade"><br><br>
<?php $euser = $this->session->userdata('email');?><input name="email" class="input" type="text" value="<?php echo $euser->email;?>" placeholder="Enter Email"><br><br>
<input type="submit" value="Update" name="submit" class="btn btn-default "/>
</form>
controller
public function edit()
{
$this->load->library('session');
$this->load->view('edit');
}
after running i get the following errors:
Severity: Notice
Message: Trying to get property 'firstname' of non-object
Filename: views/edit.php
Line Number: 33
Your post is really not so clear but the non-object error comes when you try to access an object with the [] like obj_var["index"] or an array with -> like $arr_var->index. I suggest you use var_dump($variable) on the variable you tried to access and see how to access the variable properly.
You can also cast the variable to an object or array depending on how you wish to use it.
To convert to array use the below.
$array_cast = (array) $your_variable;
or
To convert to object use the below.
$object_cast = (object) $your_variable;

How can I get the role name

<?php
$role_class = new RoleModel;
$role_name = "";
foreach ($role_class->getInfoById("role_id") as $row) {
# code...
$role_name = $row->role_name;
}
?>
<fieldset class="g12 go pb_s">
<legend>Basic Info</legend>
<div class="g12 go">
<div class="g7 go">
<div class="g2">
<label for="groupName">Role Name:</label>
</div>
<div class="g4">
<input type="text" placeholder="Type Role Name here..." name="role_name" value="<?php echo $role_name; ?>" required="required">
</div>
</div>
</div>
</fieldset>
You can use PHP's $post to retrieve the value of the input tag via the name of the HTML tag.
For Example, change the method in your form and then echo out the value by the name of the input:
In your Html
<form name="form" action="" method="post">
<input type="text" name="role_name" id="role_name" value="<?php echo $role_name;?>">
</form>
In your .php, you can get the value like below
$role_name=$post['role_name'];
Since the editor edited his questions, i will add more example.
For example, i got a list of data want to show in the html, in controller, i will put into an array first.
$form_fields = array('username', 'ref', 'password', 'password_confirm')
after that in html, i will do like this
<?php foreach($__form_fields as $field) {?>
<input type="text" placeholder="<?php echo $field?>" name="<?php echo $field? >" value="" required="required">
<?php }?>
which the above code will loop and the input field will have he name

receive post information with codeigniter

I have an order form with data. on submit the controller function new_blank_order_summary is called.
the syntax for this is:
function new_blank_order_summary()
{
echo "orderlines: ".$this->input->post(orderlines);
echo "customer: ".$this->input->post('customer');
echo "period: ".$this->input->post('period');
echo "creditlimit: ".$this->input->post('creditlimit');
$this->load->view('sales/new_blank_order_summary');
}
I cannot get the post information to display or echo? my input Name and ID is orderlines but this is not being posted or received properly.
Apologies for the 'stupid' question.
any advice welcome.
HTML
<form id="sales_blank_order_details" action="/sales/new_blank_order_summary" method="post">
<table >
<tr><td>Customer</td><td>Period</td><td>UoM</td><td>Credit Limit</td><td>Balance</td><td>New Balance</td><td>Order Lines</td><td>Round to Bundle</td></tr>
<tr><td>
<input type="text" name="customer" id="customer" value="<?php echo $customer; ?>" disabled>
</td><td>
<input type="text" name="period" id="period" value="<?php echo $period." to ".$newperiod; ?>" disabled>
</td><td>
<input type="text" name="buom" id="buom" value="<?php echo $buom; ?>" disabled>
</td><td>
<input type="text" name="creditlimit" id="creditlimit" value="<?php echo $creditlimit['creditlimit']; ?>" disabled>
</td><td>
<input type="text" name="currentbalance" id="currentbalance" value="<?php echo $creditlimit['currentbalance']; ?>" disabled>
</td>
<td>
<input type="text" name="newbalance" id="newbalance" value="<?php echo $creditlimit['currentbalance']; ?>" disabled>
</td><td>
<input type="text" name="orderlines" id="orderlines" value="1" disabled>
</td><td>
<input type="checkbox" name="rounduptobundle" id="rounduptobundle" checked>
</td></tr></table>
<input type="submit" name="blank_order_lines" id="blank_order_lines" value="Save Order and Proceed">
</form>
None of these posts are outputted by controller.
You've disabled the orderlines text input field so that's why it will not get posted. Remove disabledand it will show up in $this->input->post().
what is the name and value of your submit button. Usually the name of my submit button is name="submit" and the value is value="submit". So then I try $this->input->post('submit')=='submit'. try this in your if condition. I hope it will work. best of luck.
function new_blank_order_summary()
{
echo "orlderlines: ".$this->input->post('orderlines');
echo "customer: ".$this->input->post('customer');
echo "period: ".$this->input->post('period');
echo "creditlimit: ".$this->input->post('creditlimit');
$this->load->view('sales/new_blank_order_summary');
}
Please try this code. I didn't change a single line in it. Only I put the single quotes in the 3rd line. And, changed it's variable name is wrong so I changed it

How to Routing joomla

I have a componenet in joomla 2.5.
I have several view, in one of then I have a combobox, when I click on it, I want to call a function for that I have this
<form class="product_filter" action="<?php echo JURI::root()?>index.php/com_productos/buscarCategoria" method="POST">
<input type="hidden" class="type" name="type" value="HEALTH_FOOD"/>
<div class="select_wrapper small first">
<?php echo JHTML::_('select.genericlist', $nameCombo,'name','onChange="this.form.submit()"','value','text'); ?>
</div>
the name of my component is com_productos so in the producto.php I have this
class ProductosController extends JController
{
function buscarCategoria(){
$jinput = JFactory::getApplication()->input;
$view = $jinput->getCmd('view', 'productos');
JFactory::getApplication()->input->set('view', $view);
$model = &$this->getModel($view);
$view = &$this->getView($view, 'html');
$view->setModel($model, true);
$view->categoria();
}
but never execute this function.
Any idea
You should pass task (and controller as part of it) in the hidden field like this:
<input type="hidden" name="task" value="productos.buscarCategoria"/>
And your action can be simply index.php.
Finally I solved the problem
<input type="hidden" name="controller" value="field" />
<input type="hidden" name="option" value="com_productos" />
<input type="hidden" name="task" value="buscarCategoria" />
</form>

search form codeigniter GET_POST

Here I ant desired url like in php but how can I code this in codeigniter with controller, model, and view? someone has a code with this?
http://mysite.com/search?query=batman&cat=movie
My Controller
function index(){
//some code to get search query value
}
My Model
function getSearch(){
//some code to get search value
}
My View
<form action="http://mysite.com/search/'; ?>">
<input name="query" value="">
<input name="cat" value="">
<input type="submit">
</form>
I want also to retrieve the seesion in text field after submitting.
You should specify the method of the form
e.g.
<form action="http://mysite.com/search/" method="post">
use this to fetch the record
$query = $this->input->post('query');
$cat = $this->input->post('cat');
In order to retrieve the type text in to the text field, use set_value
e.g.
<input name="query" value="<?php set_value('cat') ?>">
<input name="cat" value="<?php set_value('query') ?>">
hope this helps!

Resources