2

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):

  1. The view for PostsController::deleteSelect() was not found.

  2. 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'));
}

}

4 Answers 4

1

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)

Sign up to request clarification or add additional context in comments.

Comments

0

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;
});

5 Comments

I put this in a form but nothing happens.When i look html source code it is look ok. Can i that(deleteSelect) make with jQuery(ajax)? how do i do that?(sry for my bad english)
Define "nothing happens". Sure, but that's a different question (you asked how to do x with cakephp, to do it differently with jquery is not cakephp specific and deserves a different and specific question).
when i click submit button and some checkbox is checked then page is refresh and all checkbox is unchecked and nothing happens.(in url is not post/deleteSelect)
so "something happens" - Sounds like you should debug and check if you missed anything in the above advice
In controler i wrote simple echo '1'; and nothing happens it won't display echo '1'. That must be a problem in submit form.
0

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.

2 Comments

i add this but I got white screen and nothing happens.
Your if statement is not catching, in that case.
0

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')
        );

    }


 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.