0

我在 cakephp 中有函数使用 ajax 和 jQuery 我将总和数据发送到我的视图和总和链接,如编辑和删除,但我无法发送删除链接

如何在我的控制器中创建链接删除

代码控制器

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'])); ?>
4

1 回答 1

1

不要直接在控制器中“回显”。为“recherche”定义一个视图并在那里进行打印。您可以使用表单助手并创建删除链接。

如果您仍然想在控制器本身中做“回声”,您可以做两件事

  1. 删除您 delete() 操作的以下行形式,并提供类似编辑的链接

    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>

所以你必须回显类似上面的内容,而不是编辑链接

于 2013-10-30T13:34:58.233 回答