Confirm popup on ajax link

I’m trying to create a confirm popup on an ajax link.

The confirm box is shown, but the ajax link is still called regardless of what is selected.

Is there a way to do this in framework or do need to write some custom jquery?

Thanks




echo (

	CHtml::ajaxLink(

		CHtml::image(

			Yii::app()->request->baseUrl . "/images/icons/delete.png",

			"Publish",

			array(

				"title"=>"Delete",

				"class"=>"grid_icon",

			)

		),

		Yii::app()->createUrl("/" . $user . "/algo/ajaxpost/"),

		array(

			"type"=>"POST",

			"data"=>array(

				"algo_id"=>$algo_model->algo_id,

				"action"=>"delete",

			),

			"success"=>"algoDelete",

		),

		array(

			"class"=>"algo_id_" . $algo_model->algo_id . " delete",

			"onclick"=>"confirm('Are you sure?\\r\\nDrafts are permanently deleted and are not recoverable.')",

		)

	)

);



try to use confirm directly in your htmlOptions and not on event such as ‘confirm’=>‘Are you sure’,

not tested

Sorry. No joy with that.

You need to return the result of confirm():

[color="#444444"][font="Arial, Helvetica, sans-serif"][size="2"]


"onclick"=>"return confirm('Are you sure?\\r\\nDrafts are permanently deleted and are not recoverable.')"

[/size][/font][/color]

Thanks for the suggestion Mike, However it now never calls the ajax link. However you got me on the right track. The following works.




"onclick"=>"if (!confirm('Are you sure?\\r\\nDrafts are permanently deleted and are not recoverable.')){return}"



… and i totally forgot that there’s also an clientchange option for this. So instead ‘onclick’ write:




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



Hi Mike

bettor suggested that, and I thought I tried it. I tried it again and it worked this time. I must have done something wrong the first time. However, this method ignores the new line characters. It just prints them as part of the confirm. The jQuery in the source is very similar, so I don’t understand why. Any ideas?


"onclick"=>"if (!confirm('Are you sure?\\r\\nDrafts are permanently deleted and are not recoverable.')){return}",

produces:


jQuery('#yt0').live('click',function(){if (!confirm('Are you sure?\r\nDrafts are permanently deleted and are not recoverable.')){return};jQuery.ajax({'type':'POST','data':{'algo_id':'4','action':'delete'},'success':algoDelete,'url':'/test/algo/ajaxpost','cache':false});return false;});



and


'confirm'=>'Are you sure?\r\nDrafts are permanently deleted and are not recoverable.'

produces:


jQuery('#yt0').live('click',function(){if(confirm('Are you sure?\\r\\nDrafts are permanently deleted and are not recoverable.')) {jQuery.ajax({'type':'POST','data':{'algo_id':'4','action':'delete'},'success':algoDelete,'url':'/test/algo/ajaxpost','cache':false});return false;} else return false;});



Only having single backslashes in the code still results in double in the source - which is what I suspect is causing the problem. Any suggestions?

Try using double quote.


'confirm'=>"Are you sure?\r\nDrafts are permanently deleted and are not recoverable."

Thanks zaccaria. That did the trick.

I really should have tried that.

Yeah… what is the difference between single and doulbe quote is still a mistery to me… I know that when one doesn.t work I can always bet on the other one…

Woa, it shouldn’t! :)

Every PHP developer should be well aware of the difference, so maybe take a quick look at this, to fill up the gaps:

http://us2.php.net/manual/en/language.types.string.php

The problem above is, that you have quoted javascript strings inside a PHP string. This can be confusing. That’s why it’s even more important that at least the PHP side is well understood.

1 Like

Check the PHP documentation for single/double quotes so that you don’t have doubts anymore - http://php.net/manual/en/language.types.string.php

In short:

Double quoted string - PHP will interpret more escape sequences for special characters and expand PHP variable names

Single quoted string - only a single quote can be escaped, escaping all other characters like \n will print the backslash too, does not expand PHP variable names

it works as ,

$name = "ABCD";

echo ‘$name’; // output : $name

echo "$name"; // output : ABCD

Sorry for posting on an old thread but this is the best one I found for this topic. Here is the code I came up with for my app:


 echo CHtml::ajaxLink("Refund Tip",array("cfgCreditTransactions/refundTip", "id" => $trans->objid,"refresh"=>0),array(),array('confirm'=>'Are you sure you want to refund this tip?'))

Which only has 1 issue, the confirmation dialog will show up many times. By that I mean you may have to click somewhere between 4 and 20 times to get the dialog to go away. Anyone else have this issue? Or see what would cause this in my code? I put this on a /site/page/about to test and it does the same thing there…TIA