How to pass the value from view page to controller - php

I am calling the controller function via ahref tag and i want to pass the current record no to the controller to update the record current value is stored in$currentID and i pass this value in input type..but i didn't get the value from the view page..i tried var_dump($id)..it shows null
Controller Code to update:
public function update($id='')
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$id=$this->input->post('update');
echo "<pre>";var_dump($id);
$dc=$this->input->post('dc');
if($dc=='c'){
$amount=$this->input->post('credit1');
}
else if ($dc=='d') {
$amount=$this->input->post('debit');
}
$data=array(
'date' =>$this->input->post('TDate'),
'code' =>$this->input->post('TName'),
'project' =>$this->input->post('TName1'),
'part' =>$this->input->post('part1'),
'part1' =>$this->input->post('part2'),
'dc'=>$this->input->post('dc'),
'amount'=>$amount,
);
$this->db->where('recno', $id);
$this->db->update('daybook', $data);
$this->session->set_flashdata('Add1', 'Updated Successfully');
redirect('BookKeeping/daybook','refresh');
}
calling update
<a href="<?=site_url('BookKeeping/update/'.$currentID)?>" class="btn btn-info btn-"><i class="icon-new position-left">
<input type="hidden" name="update" id="id" value="<?php echo $currentID?>">
</i>Update</a>
Help me to pass the currentvalue to the controller..Thanks in advance

you are sending value in url
change this
$id=$this->input->post('update');
To
$id=$this->uri->segment('3');

remove the input field. You can't pass any input data with
<i class="icon-new position-left"></i>Update
Try this
public function update($id=''){
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$id=$id;
echo "<pre>";var_dump($id);
$dc=$this->input->post('dc');
if($dc=='c'){
$amount=$this->input->post('credit1');
}
else if ($dc=='d') {
$amount=$this->input->post('debit');
}
$data=array(
'date' =>$this->input->post('TDate'),
'code' =>$this->input->post('TName'),
'project' =>$this->input->post('TName1'),
'part' =>$this->input->post('part1'),
'part1' =>$this->input->post('part2'),
'dc'=>$this->input->post('dc'),
'amount'=>$amount,
);
$this->db->where('recno', $id);
$this->db->update('daybook', $data);
$this->session->set_flashdata('Add1', 'Updated Successfully');
redirect('BookKeeping/daybook','refresh');
}

In your code
As I see your controller code posted in question, you haven't passed any value in variable $currentID which might be getting undefined, so please first try to var_dump your $currentID variable and check its value.
Do a test
Please try assigning a static id for the sake of functionality. Your static value will be passed to the method as an argument. Once you sure about static value is working, you can go ahead with finding out where is the issue in $currentID variable.
$data['currentID'] = $someValueFromDB;
$this->load->view('some_view.php',$data);
this way your value will be passed to view, make sure value is correctly passing or getting generated on view.
You are passing data in right way at controller, no problem with that.

Related

too few arguments for function error (updating data) CodeIgniter

there's an error appearing in my code for update. too few argument for function.
I searched the net and I'm not sure if I'm passing the id.
Controller:
public function update()
{
if($this->Admin_model->update($this->input->post(null, true))){
$this->session->set_flashdata('flash_msg', ['message' => 'User updated successfully', 'color' => 'green']);
} else {
$this->session->set_flashdata('flash_msg', ['message' => 'Error updating user.', 'color' => 'red']);
}
$this->admin_redirect('cms');
}
Model:
public function update($id, $data)
{
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
return $this->db->update($this->table, $data);
}
As #WILLIAM, stated in a comment, your update(...) method expects 2 parameters yet one is passed.
Change your update(...) method to this:
public function update($data)
{
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
$this->db->where('id', $data['id']);
return $this->db->update($this->table, $data);
}
This assumes that 'id' is part of your HTML form.i.e:
<input type="hidden" name="id" value="<?php echo $id ?>">
You need to call your model update function with 2 params in the controller code, but it seems you are calling it only with one parameter $data value is missing.
In below code:
if($this->Admin_model->update(**$this->input->post(null, true)**))
It should be called with 2 values first one should be an ID that identifies the record that you want to update in your model, and another should be the data that would be updated within that record.
Hope! you got the answer.

Too few arguments to function

anyone can help me, the id that I called could not be found , I'm new in codeigniter and php, and am trying to create crud (update) how to solve this? thanks in advance
Controller
public function editdata($id) ---------> LINE 41
{
$data['title'] = 'Edit Data Interview';
$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
$data['form1'] = $this->db->get_where('form1', ['id_form1' => $id])->row_array();
}
View
<form action="<?= site_url('datainterview/editdata/' . $m['id_form1']) ?>" method="post">
<input type="hidden" name="id_form1" value="<?= $m['id_form1'] ?>">
<button onclick="return confirm('Edit Data ?')" class="btn btn-primary btn-xs">Edit</button>
</form>
Error
Message: Too few arguments to function DataInterview::editdata(), 0 passed in C:\xampp\htdocs\MYNET\system\core\CodeIgniter.php on line 532 and exactly 1 expected
Filename: C:\xampp\htdocs\MYNET\application\controllers\DataInterview.php
Line Number: 41
Basically your url does not contain the id when you submit your data.
If that is a required param to perform your action that error is a good thing. If that is not a required param then you should give it a default value.
public function editdata($id = null)
{
$data['title'] = 'Edit Data Interview';
$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
$data['form1'] = $this->db->get_where('form1', ['id_form1' => $id])->row_array();
}
This way the id has the default value of null. So if you try to access that variable that would be the value you get when you access:
example.com/DataInterview/editdata
However when you access:
example.com/DataInterview/editdata/12
you get the value of 12.
You might even go a step forward and give a proper error when the id is not sent into the function. Something like:
public function editdata($id = null)
{
if (is_null($id)) {
show_404();
}
$data['title'] = 'Edit Data Interview';
$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
$data['form1'] = $this->db->get_where('form1', ['id_form1' => $id])->row_array();
}
Then you can get to that error flashdata and show proper errors to your user.

fetch value from url and pass it to another page in codeigniter

I have a url of a page as
http://localhost/projectname/admin/client/1
In the above url i have a form in which i need to fill details and save it in database. The form on this page is:
<?php
$data = array(
'type'=>'text',
'name'=>'job_title',
'value'=>'Job Title',
'class'=>'form-control');
?>
<?php echo form_input($data); ?>
<?php
$data = array(
'type'=>'submit',
'class'=>'btn',
'name'=>'submit',
'content'=>'Submit!'
);
echo form_button($data);
?>
<?php
echo form_close();
?>
On the submission of the page i wish to save it in a table but along with it i also wish to carry the id/value which is in url (in this case it is 1), and would like to carry it forward till model so that i can perform actions in database based on this id/value. Can anyone please tell how it can be done
Present code for controller
public function form()
{
$this->form_validation->set_rules('job_title','Full Name','trim|required|min_length[3]');
if($this->form_validation->run() == FALSE)
{
$regdata = array
(
'regerrors' => validation_errors()
);
$this->session->set_flashdata($regdata);
redirect('admin/clients');
}
else
{
if($this->user_model->job())
{
redirect ('admin/client');
}
}
}
Present code for model // In the users table i wish to add job title to that row where the id matches the value in url i.e 1
public function job()
{
$data = array(
'job_title' => $this->input->post('job_title')
);
$insert_data = $this->db->insert('users', $data);
return $insert_data;
}
Try some thing like this:
$clientId = $this->uri->segment('3'); // will return the third parameter of url
Now put this $clientId in form in a hidden input. Now you can get the $clientId on controller function when form submits. Pass it to model or use accordingly.

passing array value to a controller and dispalying in a view In Codeigniter

I am new to Code Igniter. I am trying to pass an array from one controller to another to another controller and then displaying those value in a view. but its not working.
Controller No. 1
if($query)
{
$data = array(
'user' => $this->input->post('email'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/member_area',$data);
}
else {
$this->index();
}
Controller No. 2 having member_area() function
function member_area()
{
$data['title'] = 'Dashboard';
$data['main_content'] = 'members_area';
$this->load->view('includes/main_template',$data);
}
And in view i am trying to display display the 'user' name received in controller one using this statement in view
echo $user;
But its not displaying user name i.e $data['user']. the view is having only values received from controller No. 2
You can define you session variables as
$this->session->set_userdata('mysession',$data);
and can access values from the session as
echo $this->session->userdata['mysession']['user'];// your email or username
You need some changes within your code. As you were calling session value as simple array. This'll work for you..
controller 1
$data = array(
'user' => $this->input->post('email'),
'is_logged_in' => true
);
$this->session->set_userdata('retrieveData',$data);
and within your view you can access that using
$retrieveData = $this->session->userdata('retrieveData'); // will return the array
$userid = $this->session->userdata['retrieveData']['user'];// retrieving single element from session
echo $userid;

how to get the value of form input box in codeigniter

value of FORM INPUT Help!!
//this is just a refrence of $nm and $fid from test_model//
$data['fid']['value'] = 0;
$data['nm'] = array('name'=>'fname',
'id'=>'id');
say i have one form_view with
<?=form_label('Insert Your Name :')?>
<?=form_input($nm)?>
and a function to get single row
function get($id){
$query = $this->db->getwhere('test',array('id'=>$id));
return $query->row_array();
}
then in controller.. index($id = 0)
and somewhere in index
if((int)$id > 0)
{
$q = $this->test_model->get($id);
$data['fid']['value'] = $q['id'];
$data['nm']['value'] = $q['name'];
}
and mysql table has something like 1. victor, 2. visible etc. as a name value
but here its not taking the value of name and id from form_input and not showing it again in form_view in same input box as victor etc so to update and post it back to database...
anyone please help!!
and please be easy as I am new to CI!!
Based on your comment to my first answer, here is a sample of a Controller, Model and View to update a user entry pulled from a table in a database.
Controller
class Users extends Controller
{
function Users()
{
parent::Controller();
}
function browse()
{
}
function edit($id)
{
// Fetch user by id
$user = $this->user_model->get_user($id);
// Form validation
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required');
if ($this->form_validation->run())
{
// Update user
$user['name'] = $this->input->post('name', true);
$this->user_model->update_user($user);
// Redirect to some other page
redirect('users/browse');
}
else
{
// Load edit view
$this->load->view('users/edit', array('user' => $user));
}
}
}
Model
class User_model extends Model
{
function User_model()
{
parent::Model();
}
function get_user($user_id)
{
$sql = 'select * from users where user_id=?';
$query = $this->db->query($sql, array($user_id));
return $query->row();
}
function update_user($user)
{
$this->db->where(array('user_id' => $user['user_id']));
$this->db->update('users', $user);
}
}
View
<?php echo form_open('users/edit/' . $user['user_id']); ?>
<div>
<label for="name">Name:</label>
<input type="text" name="name" value="<?php echo set_value('name', $user['name']); ?>" />
</div>
<div>
<input type="submit" value="Update" />
</div>
<?php echo form_close(); ?>
It's hard to see the problem from your snippets of code, please try and give a little more information as to the structure of your app and where these code samples are placed.
Presume in the last code listing ('somewhere in index') you are getting $id from the form, but you define the ID of the form input box as 'id' array('name'=>'fname','id'=>'id') rather than an integer value so maybe this is where the problem lies.
Where does the $data array get passed to in the third code listing?
From your question I think you want to display a form to edit a person record in the database.
Controller code
// Normally data object is retrieved from the database
// This is just to simplify the code
$person = array('id' => 1, 'name' => 'stephenc');
// Pass to the view
$this->load->view('my_view_name', array('person' => $person));
View code
<?php echo form_label('Your name: ', 'name'); ?>
<?php echo form_input(array('name' => 'name', 'value' => $person['name'])); ?>
Don't forget to echo what is returned from form_label and form_input. This could be where you are going wrong.

Categories