Ti Amo Laky
Ti Amo Laky

Reputation: 765

Link delete in controller cakephp

I have function in cakephp use ajax with jQuery i went send sum data to my view and sum link like edit and delete but i can't send link for delete

How I can create link delete in my controller

code controller

function recherche($desi=null,$test=null)
{
    if($desi!=null || $test!=null )
    {
        $this->Materiel->recursive = -1;
        $produits=$this->Materiel->find('all',array('conditions' => array("Materiel.name LIKE '%$desi%'")));
        echo '<table>';
        echo '<tr>
                <th>Designation</th>
                <th>Prix de vente</th>
                <th>Prix de Fornisseurs</th>
                <th>Stock</th>
                <th>quantité d\'alerte</th>
                <th>Action</th>

            </tr>';
        foreach ($produits as $produit)
        {
            $id=$produit['Materiel']['id'];
            echo "<tr>";
            echo "<td><ck id=d$id>".$produit['Materiel']['name']."</ck></td>";
            echo "<td><ck id=p$id>".$produit['Materiel']['prix']."</ck> DH</td>";
            echo "<td><ck id=p$id>".$produit['Materiel']['prixf']."</ck> DH</td>";
             echo "<td><ck id=s$id>".$produit['Materiel']['quantite']."</ck></td>";
            echo "<td><ck id=p$id>".$produit['Materiel']['souille']."</ck></td>";
            echo "<td><a href='/hossam/materiels/edit/".$produit['Materiel']['id']."'>Editer</a></td>";
            echo '</tr>';
        }
        echo '</table>';
        exit();
    }
}

Code view

    <?php
      echo $this->Html->script('ajax');
?>

<div class="materiels form">
<?php echo $this->Form->create('Materiel');?>
    <fieldset>
        <legend>
                    <?php echo __('Recherche Produit'); ?>
                </legend>
                <label for="MaterielCategories">Recherche par Designation</label>
                <input type="text" id="produit1">

                <div id="sites">

                </div>
        </fieldset>

</div>

code ajax.js

$("#produit1").keyup(function() {
    var id=$("#produit1").val();
    var image="<center><img src='/hossam/img/loading.gif' style='width: 180px;' ></center>";
    $("#sites").empty();
    $(image).appendTo("#sites");
    $("#sites").show();
    $.post(
        '/hossam/materiels/recherche/'+id+'/1',
        {
        //id: $("#ChembreBlocId").val()
        },
        function(data)
        {
            $("#sites").empty();
            $(data).appendTo("#sites");
            $("#sites").show();
        },
        'text' // type
        );
});

code link delete

<?php echo $this->Form->postLink(__('Supprimer'), array('action' => 'delete', $user['User']['id']), null, __('Are you sure you want to delete # %s?', $user['User']['id'])); ?>

Upvotes: 0

Views: 1748

Answers (1)

nithin
nithin

Reputation: 449

Do not "echo" directly in controller. Define a view for "recherche" and do the printing there. you can use Form helper and create a delete link.

If you still wants to do the "echo" in controller itself, there are two things you can do

  1. remove the following line form you delete() action, and give link like for edit

    if (!$this->request->is('post')) {
        throw new MethodNotAllowedException();
    }
    
  2. The Html for below delete link

    <?php echo $this->Form->postLink(__('Supprimer'), array('action' => 'delete', $user['User']['id']), null, __('Are you sure you want to delete # %s?', $user['User']['id'])); ?>

will be

<form action="/hossam/materiels/delete/1" style="display:none;" method="post" name="post_5271088279c63" id="post_5271088279c63">
  <input type="hidden" name="_method" value="POST">
</form>
<a href="#" onclick="if (confirm('Are you sure you want to delete # 1?')) { document.post_5271088279c63.submit(); } event.returnValue = false; return false;">Delete</a>

So you have to echo something like above not like for edit link

Upvotes: 1

Related Questions