Delete in View - javascript-conflict?

Since two days I try to find my error. I found lot’s of questions and answers in the internet. Now at the end, I need your help …

I want to have a delete-button in a view. For security-reasons I only use "Post"-requests. I tried lots of suggestions from this forum and ended up with the following … without success:

Controller:




    public function filters() {

        return array(

            'accessControl', // perform access control for CRUD operations

            'postOnly + delete', // we only allow deletion via POST request

         ); // added

    public function actionDelete($id) {

        $this->loadModel($id)->delete();

        if (!isset($_GET['ajax']))

            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

    }



View:




 <?php

    // [...] deleted html and text to keep the example short [...]


    echo CHtml::ajaxLink('X', Yii::app()->createUrl('comment/delete'), array(

    'type' => 'POST',

    'data' => 'js:{"data":' . $comment->id . '}',

             'success' => 'js:function(string){ document.getElementById("' . $comment->id . '").remove(); }'), 

             array(

                'class' => 'btn btn-danger small-btn',

                'confirm' => 'Are you sure?' //Confirmation

             ));

 ?>



I get the question "Are you sure" but after answering … no deletion happens.

I just did one other test:

commenting the following out:


// 'postOnly + delete'

Another test: deleting with CGridView works.

This seems to be important … but no idea what to do. You?

Your function filters misses a closing ‘}’.

Hi,

I hope , this is the solution for your problem




Yii::app()->createUrl('comment/delete')



REPLACE AS




Yii::app()->createUrl('comment/delete',array("id"=>$comment->id))



Analysis :

Yii::app()->createUrl(‘comment/delete’)

Above code will search delete method without any parameter inside commentcontroller. Unfortunatly you dont have those method, thats why you are getting above problem

Solution :

Yii::app()->createUrl(‘comment/delete’,array(“id”=>$comment->id))

This code will search delete method with id parameter inside commentcontroller :)

Happy coding :)

It was in my code but I didn’t copy it. Now I added it.

Very good point - I’ve realy forgotten the parameter. I added it now but the problem remained.

What goes wrong?

One more info: telling I get a 404 was a mistake. I get asked "Are you sure?" - I click yes - and nothing happens … I am still on the same page (no reload, no 400).

Not so happy coding at the moment :wink:

weird I just tested your code its working as expected

That’s really weird … I think something other in my code has negative side-affects … possibly javascript or jquerry (which I to be honest don’t realy understand fully)In the following I show you important parts of the html-output in the browser:

(the comments are not part of the source - they are for you to understand what’ going on)




<!-- 1. the jquerry.js -->

<script type="text/javascript" src="/yii/creatools/webroot/assets/490a984a/jquery.js"></script>

<script type="text/javascript" src="/yii/creatools/webroot/assets/490a984a/jquery.yiiactiveform.js"></script>


<!-- 2. Here three delete-Buttons for three comments (I have 30 instead of three on the page) -->

<a target="_blank" href="/yii/creatools/webroot/index_devel.php?r=forumkommentar/update&amp;id=2"><i class="icon-pencil"></i></a>

<a target="_blank" href="/yii/creatools/webroot/index_devel.php?r=forumkommentar/update&amp;id=3"><i class="icon-pencil"></i></a>

<a target="_blank" href="/yii/creatools/webroot/index_devel.php?r=forumkommentar/update&amp;id=4"><i class="icon-pencil"></i></a>


<!-- 3. And then a form to create a new comment -->

<form id="forumkommentar-form" action="/yii/creatools/webroot/index_devel.php?r=forum/forum&amp;ebene=8&amp;id=8" method="post">

  <textarea class="span8" rows="10" name="Forumkommentar[text]" id="Forumkommentar_text"></textarea>

  <div class="row buttons">

  <input type="submit" name="yt22" value="Kommentar speichern" />

  </div>

</form>


<!-- 4. javascript-Code generated from Yii -->

<script type="text/javascript" src="/yii/creatools/webroot/assets/d261d46b/main.js"></script>

<script type="text/javascript">

/*<![CDATA[*/


$('.search-button').click(function(){

	$('.search-form').toggle();

	return false;

});

$('.search-form form').submit(function(){

	$('#forum-grid').yiiGridView('update', {

		data: $(this).serialize()

	});

	return false;

});


<!-- 5. the code which should trigger the delete  (generated with ajaxlink) -->

jQuery('body').on('click','#yt0',function(){if(confirm('Are you sure?')) {jQuery.ajax({'type':'POST','data':{"data":2},'success':function(string){ document.getElementById("2").remove(); },'url':'/yii/creatools/webroot/index_devel.php?r=forumkommentar/delete&id=2','cache':false});return false;} else return false;});

jQuery('body').on('click','#yt1',function(){if(confirm('Are you sure?')) {jQuery.ajax({'type':'POST','data':{"data":3},'success':function(string){ document.getElementById("3").remove(); },'url':'/yii/creatools/webroot/index_devel.php?r=forumkommentar/delete&id=3','cache':false});return false;} else return false;});

jQuery('body').on('click','#yt2',function(){if(confirm('Are you sure?')) {jQuery.ajax({'type':'POST','data':{"data":4},'success':function(string){ document.getElementById("4").remove(); },'url':'/yii/creatools/webroot/index_devel.php?r=forumkommentar/delete&id=4','cache':false});return false;} else return false;});


<!-- 6.Code to validate the form-->

jQuery('#forumkommentar-form').yiiactiveform({'attributes':[{'id':'Forumkommentar_text','inputID':'Forumkommentar_text','errorID':'Forumkommentar_text_em_','model':'Forumkommentar','name':'text','enableAjaxValidation':true,'summary':true},{'summary':true},{'summary':true},{'summary':true},{'summary':true}],'summaryID':'forumkommentar-form_es_','errorCss':'error'});

/*]]>*/

</script>



Hope, now it’s no longer a miracle for an experienced javascript’ler :slight_smile:

Push me in the right direction, please.

To come closer to a solution I’ve got the idea to reduce more of the code above:

I deleted:

  • the other delete-button - so there is only one left

  • the form and the jquery form-validation




<!-- 1. the jquerry.js -->

<script type="text/javascript" src="/yii/creatools/webroot/assets/490a984a/jquery.js"></script>

<script type="text/javascript" src="/yii/creatools/webroot/assets/490a984a/jquery.yiiactiveform.js"></script>


<!-- 2. Here is a delete-Buttons for deleting a comment -->

<a target="_blank" href="/yii/creatools/webroot/index_devel.php?r=forumkommentar/update&amp;id=2"><i class="icon-pencil"></i></a>


<!-- 3. javascript-Code generated from Yii -->

<script type="text/javascript" src="/yii/creatools/webroot/assets/d261d46b/main.js"></script>

<script type="text/javascript">

/*<![CDATA[*/


<!-- 4. the code which should trigger the delete  (generated with ajaxlink) -->

jQuery('body').on('click','#yt0',function(){if(confirm('Are you sure?')) {jQuery.ajax({'type':'POST','data':{"data":2},'success':function(string){ document.getElementById("2").remove(); },'url':'/yii/creatools/webroot/index_devel.php?r=forumkommentar/delete&id=2','cache':false});return false;} else return false;});

/*]]>*/

</script>



… and still nothing happens when I push the delete-button.

What else information do you need to find out what’s going wrong?

Now I am a little bit further. I installed firebug

With the code above I get the following errors on the try to delete:

When I delete the same id with CgridView there is no problem … it works.

So I am a little bit further now … but don’t know how to solve my problem :frowning:

I have no more ideas.

Who can help to find the error?

Stop … :slight_smile:

I have found something … I’ll be back in about 30 minutes, hopefully with an answer.

There I am back :slight_smile:

The solution: ajaxLink does not send the CSRF-token from itself. So I had to add it as a parameter:


'params' => array('YII_CSRF_TOKEN' => Yii::app()->request->csrfToken)

And here as a full example in a view:




<?= echo CHtml::linkButton('Delete', array(

   'submit' => $this->createUrl('forumkommentar/delete', array("id" => $kommentar->id)),

   'confirm' => "Are you sure want to delete?",

   'csrf' => true, // the following 'params' does the same

// 'params' => array('YII_CSRF_TOKEN' => Yii::app()->request->csrfToken)

));

?>



Hope it also helps others.

But with ajaxLink it does not work … still nothing happens:




  <?php

    echo CHtml::ajaxLink('X', Yii::app()->createUrl('comment/delete'), array(

    'type' => 'POST',

    'data' => 'js:{"data":' . $comment->id . '}',

    'success' => 'js:function(string){ document.getElementById("' . $comment->id . '").remove(); }'), 

          array(

             'class' => 'btn btn-danger small-btn',

             'confirm' => 'Are you sure?', //Confirmation

          ));

 ?>



Clicking on "X" for delete … nothing happens. With firebug I still get:

"Der CSRF-Token konnte nicht verifiziert werden."

“The CSRF-token couldn’t be verified.”

If I replace data with this, “delete” runs but then teh page isn’t refreshed.

So how can I combine the following two "datas"?


    

 'data' => 'js:{"data":' . $comment->id . '}',

 'data' => array('YII_CSRF_TOKEN' => Yii::app()->request->csrfToken),



I have tried this without success:


'data' => array('YII_CSRF_TOKEN' => Yii::app()->request->csrfToken, 'js:{"data":' . $comment->id . '}'),

?

I don’t find a solution and I am near to give up :frowning: … hope there will come an impulse from reality, virtuality or heaven :slight_smile:

Either




 'data' => 'js:{"data":' . $comment->id . ', "YII_CSRF_TOKEN": ' . Yii::app()->request->csrfToken . '}',



or




'data' => array('YII_CSRF_TOKEN' => Yii::app()->request->csrfToken, 'data' => $comment->id ),