【问题标题】:Link delete in controller cakephp控制器 cakephp 中的链接删除
【发布时间】:2013-10-30 11:59:38
【问题描述】:

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

【问题讨论】:

    标签: php ajax jquery cakephp


    【解决方案1】:

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

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

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

      if (!$this->request->is('post')) {
          throw new MethodNotAllowedException();
      }
      
    2. 以下删除链接的 Html

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

    将会

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-15
      • 1970-01-01
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多