I have a list of post and i want to delete them using multiple checkboxes.
I followed this link Multiple Check boxes in cake php
but i get this error(i use cakephp 2.4):
The view for PostsController::deleteSelect() was not found.
Confirm you have created the file: C:\xampp\htdocs\cakephp2\app\View\Themed\Cakestrap\Posts\delete_select.ctp
I want to access this data from index.ctp not from delete_select.ctp. My question is how i access this data "data['Post']['box'][]"?
My code is:
index.ctp
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $this->Html->link($post['Post']['title'], array('action' => 'view', $post['Post']['id'])); ?>
</td>
<td>
<?php echo $post['Post']['created']; ?>
</td>
<td>
<?php echo $this->Form->checkbox('post',
array(
'value' => $post['Post']['id'],
'name' => "data['Post']['box'][]",
));?></td>
<td>
<?php echo $this->Form->postLink(
'Delete',
array('action' => 'delete', $post['Post']['id']),
array('confirm' => 'Are you sure?'));
?>
<?php echo $this->Html->link('Edit', array('action' => 'edit', $post['Post']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
<p><?php echo $this->Html->link('deleteSelect', array('action' => 'deleteSelect')); ?></p>
deleteSelect function
public function deleteSelect(){
if(!empty($this->data)) {
foreach($this->data['Post']['box'] as $key => $value){
$this->Post->delete($value);
}
$this->redirect(array('action' => 'index'));
}
}
you have to include all your checkboxes in a form if you want some data passed to your action.
But you can't do this since you are using Form::postLink that creates a form and you can't nest a form inside another form.
So you have to get rid of your postLinks. Are you sure you need them? Can't them all be simple links?
Once you have removed your postlinks then you can put all your code inside a big form
echo $this->Form->create('Post', array('action' => 'deleteSelect'));
// your foreach code here
echo $this->Form->end('Delete selected posts');
also: in your controller put this piece of code
$this->redirect(array('action' => 'index'));
outside the if condition
so the page will be redirected even if no data is passed (no checkbox is checked)
There is no post data
The last line of the view file is:
<p><?php
echo $this->Html->link(
'deleteSelect',
array('action' => 'deleteSelect')
);
?></p>
Unless there is some javascript listening for a click - that's just a link, not something which submits a form as such that means there is no form data. Given that, the relevant controller action does not enter the appropriate if and therefore attempts to render a view file:
public function deleteSelect(){
if(!empty($this->data)) {
...
// Unreachable
$this->redirect(array('action' => 'index'));
}
}
To prevent the problem mentioned in the question - simple don't make redirecting dependent on the presence of form data:
public function deleteSelect(){
if(!empty($this->data)) {
...
}
// Always executed
$this->redirect(array('action' => 'index'));
}
But that won't address the main problem that as written, it will simply do nothing.
Inputs need to be in a form
For inputs to do anything (ignoring the use of javascript) they need to be in a form. Therefore, the raw html needs to change from:
<table>
...
<input type="checkbox">
...
<input type="checkbox">
...
<input type="checkbox">
</table>
<p>
<a href="/deleteSelect" ...>deleteSelect</a>
</p>
To:
<form action="/deleteSelect" ...>
<table>
...
<input type="checkbox">
...
<input type="checkbox">
...
<input type="checkbox">
</table>
<input type="submit" value="deleteSelect">
</form>
I.e.:
Wrap the table in a form
Define the form action to go to the appropriate function
The link must change to a submit button.
In this way, the desired result can be achieved.
Warning - nested forms
Know that it's not valid to put a form inside another form. Therefore to get the "multi-delete" function to work using a normal form, will require removing the individual delete buttons since they are also embedded forms. An alternative technique to using postLink would be to make them normal links and use a simple javascript handler to prevent submitting via get, for example:
$('a.delete').click(function(e) {
if (confirm('Sure?')) {
$.post(this.attr('href')});
}
return false;
});
CakePHP requires that, if you're calling a function in a controller, you have a corresponding .ctp file, as noted.
To get around this you can use $this->autoRender = false; inside your method.
i have implemented it in my code just see my code.
<div>
<?php //echo $this->Form->create();
echo $this->Form->create(null, array(
'url' => array('controller' => 'manageParcels', 'action' => 'deleteParcel')
));
?>
<table width="100%" border="1">
<tr>
<th></th>
<th>Parcel Number</th>
<th>Consignment Number </th>
<th>Customer Name </th>
<th>Customer Address </th>
<th>Customer Phone-Number </th>
<th>Customer EmailId </th>
</tr>
<?php foreach($parcelDatas as $parcelData){
?>
<tr>
<td><input type="checkbox" name ="status[]" value="<?php echo
$parcelData['ManageParcel']['id']; ?>"></input></td>
<td align='center'><?php echo $this->html->link($parcelData['ManageParcel']
['parcelNo'], array('action' => 'editParcel',$parcelData['ManageParcel']['id']),
array('escape' => false));?> </td>
<td align='center'><?php echo $parcelData['ManageParcel']['ConNo']; ?></td>
<td align='center'><?php echo $parcelData['ManageParcel']['cusName']; ?></td>
<td align='center'><?php echo $parcelData['ManageParcel']['cusAddress']; ?></td>
<td align='center'><?php echo $parcelData['ManageParcel']['cusPhone']; ?></td>
<td align='center'><?php echo $parcelData['ManageParcel']['cusEmail']; ?></td>
</tr>
<?php
}?>
</table>
<?php
echo $this->Form->end(__('Delete Parcel')); ?>
</div>
**My controller code**
public function deleteParcel()
{
$this->autoRender=FALSE;
if ($this->request->is('post'))
{
if(empty($this->request->data))
{
$this->Session->setFlash(__('Please select parcel '));
return $this->redirect(
array('controller' => 'ManageParcels', 'action' =>
'listParcel')
);
}
$deleteParcels=$this->request->data['status'];
$size=sizeof($deleteParcels);
foreach ($deleteParcels as $deleteParcel)
{
$this->ManageParcel->id = $deleteParcel;
$parcelData=$this->ManageParcel->findById($deleteParcel);
if ($this->ManageParcel->delete()) {
$this->recordActivity('deleteParcel','Parcel
Number '.$parcelData['ManageParcel']['parcelNo'] . ' deleted' );
$this->Session->setFlash(__('Parcel data deleted'));
}
else {
$this->Session->setFlash(__('Parcel data was not Deleted'));
return $this->redirect(array('action' => 'listParcel'));
}
}
$this->recordActivity('deleteParcel',$size.' Parcels data deleted ');
return $this->redirect(
array('controller' => 'ManageParcels', 'action' => 'listParcel')
);
}
}
Related
I am making a table from a database and want to add an Edit/delete button to each row update or delete a specific row from the database. I have successfully added working "delete" button but I have no idea how could I update data in table <td> in view and send it to controller.
Here is my code:
view file
<table class="table table-striped">
<tr>
<td>name</td>
<td>age</td>
<td>gender</td>
<td>class</td>
<td>roll no.</td>
</tr>
<?php foreach($record as $r): ?>
<tr>
<td><?php echo $r->name; ?></td>
<td><?php echo $r->age; ?></td>
<td><?php echo $r->gender; ?></td>
<td><?php echo $r->class; ?></td>
<td><?php echo $r->roll no; ?></td>
<td><a href="" >Edit</a>
<a href="<?php echo base_url()."student/deleteRow" ?id="$r->name">"
onclick="return confirm
('Are you sure to Delete?')"><i class="icon-trash"></a></td>
</tr>
<?php endforeach; ?>
</table>
Controller Function
public function deleteRow(){
if(isset($_GET['id'])){
$id=$this->input->get('id');
$this->student_model->rowDelete($id);
redirect($_SERVER['HTTP_REFERER']);
}
}
I don't know how can I now insert an input field to update table row without effecting previous view. Any suggestion would be helpful.
To Edit the Studen data you need to pass an id or uniue column name to the data base to get that student data.
First Set the student id in <a href=""> tag.
<td><a href="<?= base_url('student/edit_student') ?>/$r->id" >Edit</a>
Then When you click on the edit it will take you to the controller. You can get the third url parameter direct in as show in the controler code:
You can also use get as shon
Your Controller should be:
public function edit_student($id){
$student_data = $this->student_model->get_student_data($id);
$this->load->view('your_view',['student_data'=>$student_data)]);
}
Here is you model which get the id form controllr and find the student data and passit to back to the controller:
Your Model should be:
public function get_student_data($id){
$this->db->select('*');
$this->db->from('your_table_name');
$this->db->where('id',$id);
$query = $this->db->get();
$student_data = $query->$row_array();
if(isset($student_data) && !empty($student_data)){
return student_data;
} else {
return FALSE;
}
}
Form controller you pass the data to the view.
On View Side:
<?php
// Just to ensure the data. Comment it after testing
echo "<pre>";
print_r($student_data);
echo "</pre>";
?>
<form action="<?= base_url('student/update_student') ?>/<?= $student_data['id'] ?>">
<input name="your_column_name" value="<?= $student_data['your_column_name'] ?>">
// Try to do the same for all you column.
<input type="submit" value="updata">
</form>
Here is the controller for update the data
public function update_student($id){
$student_data = $this->input->post();
$status = $this->student_model->update_student($id,$student_data);
if($status == TRUE){
$this->load->view('your_view');
// Your Success view
} else {
// Your view if fail to update
$this->load->view('your_view');
}
}
Here is the model for update the data
public function get_student_data($id,$student_data){
$this->db->where('id',$id);
$this->db->update('your_table_name',$student_data);
if($this->db->affected_rows() == 1){
return TRUE;
} else {
return FALSE;
}
}
Very similar to what you have done for delete. Something like this:
<td>
<a href="" >Edit/Delete</a>
<!-- This should be another method in Student controller -->
<i class="icon-trash"><!-- I changed order of edit and delete -->
</td>
I need to warn you for CSRF. If you don't implement better security here, anyone pointing to that link would be able to edit or delete data.
Check Security class in documentation and how to set hidden value so that way you would ensure that only one who has already requested that page was able to edit/delete rows.
<td><a href="<?php echo base_url();?>controller_name/function_name/<?php echo $edit_id;?>" >Edit</a></td>
another way
<td><a href="<?php echo base_url();?>controller_name/function_name?id=<?php echo $edit_id;?>" >Edit</a></td>
You can use any one according to your requirement.
I have form where the select boxes are there and select all option, i want to post all the data without click on check boxes, i dont want to view this page, mean if user go to signup page the data will already selected by default how can i do that wothout any view?
this is my view code
<?php v_start($this);?>
<?php
#if(element exits of controller-action)
echo $this->element('validations/users/signup');
?>
<script>
function toggleChecked(status)
{
jQuery(".new-checkbox input").each( function() {
jQuery(this).attr("checked",status);
}
)
}
</script>
<h1><span style="color:CornflowerBlue"><?php echo __l('Step1');?></span></h1>
<?php
$form = $this->FormManager;
echo $form->create('User',array_merge($form_options,array('default'=>true)));
?>
<table width = "100%">
<tbody>
<tr>
<td>
<?php echo $form->input('',array('id'=>'mark-all','onclick'=>'toggleChecked(this.checked)','type'=>'checkbox','label'=>'Select All Products' ));?>
<?php echo $form->input('product_id',
array(
'multiple'=>'checkbox',
'options'=>$products,
'div'=>false,
'class'=>'new-checkbox'
)
);
?>
</td>
</tr>
<tr>
<td>
<?php
echo $this->FormManager->end(LBL_BTN_NEXT);
?>
</td>
</tr>
</tbody>
</table>
i dont want this page, the user will select the data by default.
this is my controller code
$products = $this
->User
->ProductsUser
->Product
->find('list',array(
'fields' => 'product_id,name',
'conditions'=>array(
'Product.is_visible'=>true
)
)
);
$this->set('products',$products);
if($this->request->is('post'))
{
$this->Session->write('signUp.signUpProduct',$this->request->data['User']);
$this->redirect(array('action'=>'signup_product'));
}
}
how can i do that, Thanks in advance.
The attr() doesn't take true or false as an argument.
jQuery(this).attr("checked","checked"); // Correct usage of attr()
Although I think attr is deprecated for the favorable prop() method.
jQuery(this).prop("checked",status);
Which indeed does take a true/fales argument. :)
Hope this helps.
I have a view which has 2 forms:
<table>
<th>Write a comment.</th>
<tr>
<td>
<?php echo form_open($this->uri->uri_string(),$form1);
echo form_textarea($comment);
echo form_submit('submit','submit');
echo form_close();
?>
</td>
</tr>
</table>
<table>
<tr>
<td>
<?php echo form_open($this->uri->uri_string());
echo form_dropdown('portion', $portion_options);
echo form_submit('book','book');
echo form_close();
?>
</td>
</tr>
</table>
In the controller I check which button was clicked and then I perform some action by adding the corresponding form's values to the database.
if(isset($_POST['book']))
{
//sending the data to the database
echo "Book button clicked";
}
if(isset($_POST['submit']))
{
//sending the data to the database
echo "Submit button clicked";
}
However when the 'book' button is clicked no action is performed. It is like the button was never clicked. Whereas when I click the 'submit' button, every action is done properly.
In the past I have used the same technique on plain php (i mean no framework, just php), and has worked fine for me. Does codeigniter need any further configuration? Am I doing something wrong?
Why not add a hidden field to both forms called form_idwith values 1 and 2 respectively? Easy to catch in your controller upon post; e.g.:
if($this->input->post()){
switch($this->input->post('form_id')){
case 1:
// do stuff
break;
case 2:
// do stuff
break;
}
}
<?php echo form_open($this->uri->uri_string(),$form1);
and
<?php echo form_open($this->uri->uri_string());
Looks like you forgot to provide the settings in the second one like:
<?php echo form_open($this->uri->uri_string(),$form2);
Well after spending my whole day on this I finaly managed to solve it somehow (although I believe it is not such a propper way to handle this).
Well:
$comment = array(
'name' => 'comment',
'id' => 'comment',
'value' => 'write you comment',
'row' => '5',
'cols' => '100'
);
<table>
<th>Write a comment.</th>
<tr>
<td>
<?php echo form_open($this->uri->uri_string());
echo form_hidden('form_id', 1);
echo form_textarea($comment);
echo form_submit('submit','submit');
echo form_close();
?>
</td>
</tr>
</table>
<table>
<th>Write a comment.</th>
<tr>
<td>
<?php echo form_open($this->uri->uri_string());
echo form_hidden('form_id', 2);
echo form_dropdown('comment', $portion_options);
echo form_submit('book','book');
echo form_close();
?>
</td>
</tr>
</table>
Probably the forms fields (textarea and dropdown) needed to have the same name (which i have set to 'comment'). Although I do not understand why :/
Thank you all for trying to help me :)
Using CodeIgniter I am trying to create a link that the user can click within a view to show them the details of a record in my database.
In ASP.NET MVC3 with C# I would do it with #Html.ActionLink("Controller", "Action", new { id = item.Id})
This is my session controllers index() function
public function index()
{
$data['sessions'] = $this->session_model->get_sessions();
$this->load->view('_header');
$this->load->view('session/index', $data);
$this->load->view('_footer');
}
This is the index view it loads where I want to be able to click the link to go to enter() function
<table>
<th>Session Name</th>
<th>Description</th>
<th>Host</th>
<th></th>
<?php foreach ($sessions as $session_item): ?>
<tr>
<td> <?php echo $session_item['sessionName'] ?> </td>
<td> <?php echo $session_item['sessionDesc'] ?> </td>
<td> <?php echo $session_item['adminName'] ?> </td>
<td> <a id="enterSession" href=<?php echo site_url("session/enter" + $session_item['id']) ?> >Enter</a></td>
</tr>
<?php endforeach ?>
The enter session points me to the url "http://192.168.1.36/index.php/1" (if the id of my item is 1) whereas I expect "http://192.168.1.36/index.php/session/enter/1"
Any ideas on how I can make it call the enter() function also in the session controller (shown below)
public function enter($id) {
$this->load->view('_header');
$this->load->view('session/enter');
$this->load->view('_footer');
}
There seems to be a typo in the string concatenation in your PHP code. Perhaps it will work to use:
site_url("session/enter" . $session_item['id'])
... rather than a + sign between the two strings.
Regarding the second question - it looks correct as-is. Is it failing to call the session controller's enter() function passing the $id as an argument (assuming the URL is correct)?.
i am trying to implement jquery datatable, in my cakePHP based website, but it just wont load. this website is already half developed, and from the way i see it, the js' is loaded through a file called _head.inc.ctp located inside the views/layouts folder, i have added the datatables library inside the libs folder which is webroot/js/libs and load it inside the _head.inc.ctp file.
suppose i have this:
my controller:
var $helpers = array(
'Form',
'Html',
'Javascript'
);
//my method
function dataTable_example($id=null){
$details = $this->Detail->find("all");
$this->set('details', $details );
}
my view:
<div>
<?php echo $javascript->link('libs/jquery.dataTables.js'); ?>
<script>
$(document).ready(function(){
$('#js-datatable').dataTable();
});
</script>
<h2><?php echo __l('Tickets');?></h2>
<div>
<table id="js-datatable">
<tr>
<th>some heading 1</th>
<th>some heading 1</th>
<th>some heading 1</th>
</tr>
<?php
if (!empty($details)){
foreach ($details as $detail):
?>
<tr>
<td><?php echo $detail['Detail']['id'];?></td>
<td><?php echo $detail['Detail']['created'];?></td>
<td><?php echo $detail['Detail']['ticket_detail'];?></td>
</tr>
<?php
endforeach;
}else{
?>
<tr>
<td>No Data Found</td>
</tr>
<?php }?>
</table>
</div>
</div>
i even hard coded it using the usual call, and checked it using firebug to see if the script is loaded or not, and according to firebug, it is loaded, so i cant see whats making the script fail my table.
did i missed some steps ? please help
thanks
You don't have thead and tbody elements as required by the datatables script
You should use the find function in your controller and pass the array to the view and in the view write it.. don't just leave the table empty