edit form in CodeIgniter - php

I am newbie in CodeIgniter. I have a table where data is showing. In each row there is an edit button. When the edit button is clicked, a modal popup will pop up and then you are able to edit it. What my problem is, is that I don't know how to attach a link of my controller behind the image so that I can send the row id to the controller. I link my image(Edit) to the modalpopup in the href .. so how can I perform both the operations at the same time ..
This is my link in form view. As you can see, in the href tag, I link to myModal where the edit form is displayed, but I want that edit form to display against the row. So I have to link it to the controller, which I don't know how to do.
Is there a way to attach links to an image or there is another way ?
<a data-toggle="modal" href="#myModal">
<img src="images/32/edit.png" alt="Edit"></a></td>
<div class="modal hide" id="myModal">
//here all the form fields are displaying

Follow these steps
Create a link like this
<a data-toggle="modal" href="<?php echo site_url('controllername/methodname/'.$id);?>">
<img src="images/32/edit.png" alt="Edit">
</a>
Now see controller class's method will be called on click. the id should be provided via loop.
Controller method
function methodname(){
$id = $this->uri->segment(3);
$this->load->model('modelname');
$data['result'] = $this->modelname->getResord($id);
$this->load->view('editform',$data);
}
And model method should fetch data with where condition
function modelmethod($id)
{
return $this->db->where('id',$id)->get('tablename')->row();
}

Related

How to add button for every field when retrieving data from a database

I have looked everywhere for this question, perhaps I'm just not phrasing it correctly.
I have a page displaying various products, the table is called items. There in my fields I have all the information and images to display on the page, this works fine. Now I want to be able to press a button to add the item to cart, I have the code below where I access the data.
<div class="col-md-5">
<h2 style="font-size:25px;"> <?=$items['title'];?></h2>
<img src="<?=$items['image'];?> "width='300' height='500'/>
<p style="font-size:20px;" class = "lprice">GBP <?= $items['price'];?></p>
</div>
Now that I have done that, my question is how can I add a button for every time a result is displayed and have that button linked with that item, I appreciate this may no be as straight forward as I am phrasing this, but just looking for some help.
Thanks
You can do it in two ways, either way, you need a primary key (eg: id) in your items table which will hold a unique number for each of your items.
After that, you print your id along with everything else inside a button or inside an a tag. Like
<div class="col-md-5">
<h2 style="font-size:25px;"> <?=$items['title'];?></h2>
<img src="<?=$items['image'];?> "width='300' height='500'/>
<p style="font-size:20px;" class = "lprice">GBP <?= $items['price'];?></p>
<a class="btn btn-primary" href="add_to_cart.php?id=<?=$items['id'];?>">Add to Cart</a>
</div>
Now inside your add_to_cart.php file, you can receive the id of the products like this $product_id = $_GET['id'];
Once you have the id of the product, you can process the rest on the add_to_cart.php file and redirect the user back to the previous page with a success or error message.
This way, we passed the id using a URL parameter. The second way is to pass the id using a form. I just wanted to let you that this method exists. I hope this helps. If you have any doubt, feel free to ask in the comment section.

PHP Codeigniter asigning controller to a html element

I am new to Codeigniter and I am wondering how can i add a controller to a html button or div element. I want for example to have a div element which on click to ask for a controller and so on. Tried with a button with a action="controller/function" but couldn't succeed.
You can use site_url() like this
<a href="<?php echo site_url('controllername/methodname');?>" >Click Me</a>
Or direct an anchor
<?php echo anchor('controllername/methodname');?>
And in the form you can use form_open().
<?php echo form_open('controllername/methodname');?>
Reference
URL Helper
And
Form Helper

Submitting a form when calling Bootstrap modal

im new to bootstrap, and i want to do this:
when i press a button i want to submit the form, and then open a modal, how can i do this? so i get the post data on the modal. I've tried with two buttons, first one submit the form and the other one opens the modal, if i do it in that order it works fine, but i would like to do it in only one button.
any help?
this is my button:
<a data-toggle="modal" href="#myModal" data-id="<?php echo $rol['rol_id'];?>" class="open btn btn-primary btn-lg">Launch demo modal</a>
This is a bad method and you should be using AJAX for this instead. When the modal is clicked, an ajax request should fire. Wait for its response (and return the data you need). If its successful then load the modal with the returned data.
I've tried with two buttons, first one submit the form and the other
one opens the modal, if i do it in that order it works fine, but i
would like to do it in only one button.
From this I assume that after the POST you display the returned data within the modal. If you want to follow this method you will need to write some Javascript within the HTML to fire the modal after a POST request.
<?php if ($_POST['submitted']) { ?>
$(document).ready(function() {
$("#myModal").modal();
});
<?php } ?>
I would recommend against this as you have to put this in a .php or .phtml file, AJAX would be better suited in this case.
// This question has already been solved elsewhere... I suggest following this link and doing what #Samuel Liew recommends. AJAX/jQuery/PHP Form submission and modal box open on success #fruitp is correct AJAX is the way to go with this.

get old values in a form in Colorbox before update

i am new bie in code igniter .i have a data showing in tables in my view page .. in each row there is an edit button .. i want that if i clicks on the edit link a colorbox is gonna pop up with a form in which the form is filled with old values and he then able to update it.. i have done everything in controller and model but i dont know how to use colorbox .. i visit their site also but i didnt understand much .. this is what i am doing
here is the form where user add the data
form name and id is "form"
echo form_open('employesController/addEmployes')
form_input();//
form_input();//
form_button()
form_close();
this is the table where data is showing
<td><?php echo $row->emp_name ?></td>
<td><?php echo $row->description ?></td>
<td> <a href = "employesController/editEmploye/<?php echo $row->emp_id ?>" id = "btn"> Edit
my controller
function editEmploye($id){
$this->load->model('employesModel');
$data['result'] = $this->employesModel->getEditEmploye($id);
echo json_encode($data);
}
my modal
function getEditEmploye($id)
{
return $this->db->where('emp_id',$id)->get('employees')->row();
}
and i thing my jquery function for popup modal is going to be like that ..
$(document).ready(function(){
$("#btn").click(function(){
var $form = $("#form");
$("#").colorbox({inline:true, href:$form});
});
});
please tell me how can i get selected row values in a form in Colorbox
Ok, Colorbox and codeigniter really work well together. I have done several projects in CI that have used Colorbox.
Colorbox will just load a url within an iframe.
so for example.
http://localhost/mycontroller/edit
without colorbox it would change in the current window. If you include color box class on the actual link it should open within the iframe.

load pages dynamically using jQuery $.post in codeigniter

I have a home page in which i have two DIVs (left & right).
in left div i will be having some items. and in right div its details.
(please see the image)
here, clicking on an item in the left div, its corresponding details should get loaded.
i have two separate views for the two divs. I am able to load the left view from the controller function.
But I should load the right div based on the item_id of the left div item without any page refresh.
for that I will send item_id via jQuery $.post and get the details_page.
my view of the details page is as fillows
<div class="title"><?php echo $title; ?></div>
<div class="details">
<div class="author"><?php echo $author; ?></div>
<div class="pagedata"><?php echo $pagedata; ?></div>
</div>
these PHP variables should get replaced when details page gets loaded.
Please help me in coding the above scenario using jQuery in codeigniter.
In your PHP controller, you need a method that serves the views :
class Foo extends CI_Controller {
public function getItem($item) {
// the view holds the html for the right div
$this->load->view($item);
}
}
in jquery, you can then fetch the view like this:
$.get('example.com/foo/getItem/item1', function(resp){
$('#contentDiv').html(resp);
}, 'html');
You would, of course, call this jquery code snippet everytime a pane on the left div is clicked with the appropriate item name.

Categories