2013-10-30 74 views
0

我在CakePHP中使用了Ajax使用jQuery我去功能和數據發送到我的觀點,並和鏈接像編輯和刪除,但我不能刪除鏈接刪除控制器CakePHP的

發送鏈接如何創建鏈接刪除我的控制器

代碼控制器

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

代碼視圖

<?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> 

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

代碼鏈接刪除直接在控制器

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

回答

1

不要 「回聲」。爲「recherche」定義一個視圖並在那裏進行打印。你可以使用Form幫助器並創建一個刪除鏈接。

如果你仍然想在控制器本身做了「回聲」,有兩件事情可以做

  1. 刪除以下行的形式刪除()動作,並給出鏈接像編輯

    if (!$this->request->is('post')) { 
        throw new MethodNotAllowedException(); 
    } 
    
  2. 的HTML下面刪除鏈接

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

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

所以,你必須呼應像上面不喜歡的編輯鏈接