ajaxSubmitButton doesn't submit as AJAX?

Hi there,

I’m sure that I made a mistake, but simply can’t find it. And since I’ve never used AJAX in Yii (an fairly never in PHP at all) I fear that without help from someone else, I’ll be looking for it till next Christmas! :confused:

This is the simplest as it can be! Viewer:


<div id="section_2_div" class="ajaxcontent"><?php <img src="gfx/ajaxload.gif"></div>


echo CHtml::ajaxSubmitButton

(

	'Submit request',

	array('zlecenia/ajaxgetparams'),

	array(

		'update'=>'#section_2_div',

            	'complete'=>'function(msg) {alert(msg.responseText)}',

	)

);


?>

Controller:


class ZleceniaController extends Controller

{

    	public function actionAJAXGetParams()

    	{

            	if(isset($_POST['ajax']))

            	{

                    	echo date('H:i:s');

                    	Yii::app()->end();

            	}

            	else throw new CException('Not an AJAX!');

    	}

}

Problems? Two:

[list=1][]In response I got my own exception text (Not an AJAX!) and I’m stuck! What? CHtml::ajaxSubmitButton doesn’t submit as AJAX?[]’#section_2_div is not being updated! Even if I see (and click OK) message displayed upon complete, it still remains with the loader.[/list]

What am I missing?

Where did you set $_POST[‘ajax’] ?? :) you have to set what you wont to post, ajax data, or send it via get :)

Thanks! As I said, I have no experience with using AJAX calls and just copied this part from another piece of code generated by Yii, mistakenly assuming, that this array is auto-filed by Yii performing AJAX request. Of course, I should use: Yii::app()->request->isAjaxRequest in that place.

Now works fine, but how to change that example code from being ajaxbutton to pure AJAX call initiated upon page load? Tired this:


CHtml::ajax(array

(

    	'url'=>'zlecenia/ajaxgetparams',

    	'update'=>'#section_2_div',

    	'complete'=>'function(msg) {alert(msg.responseText)}',

));

But this doesn’t work, again with the same effect (or rather to say - lack of effect) - i.e. no message is displayed and div isn’t updated.

EDIT: alex-w told me here to use direct jQuery function declaration, without using CHtml:ajax(). Which approach is better?

CHtml::ajax just generate the ajax call scritp, but someone has to call.

For example, you can put in a button:




CHtml::button(..., array('onClick'=>CHtml::ajax(...)))



This has the same effect of an ajax button, or you can put on onmouseover of something, whereever you want.

You can use firebug for test if the request was sent or not, if it was sent if it was successfully and so on (I think is rather impossible to debug ajax without firebug).

And is a bit strange to use both update and success at a time, you should choose one of them (in your case, update is ignored).

If you use ajaxsubimtbutton you have to use submit form, and you have to use $htmlOptions[‘params’], and $htmlOptions[‘return’], or you have to set ajax options, witch are same

clientchange(); //$htmlOptions[‘ajax’] witch calls CHtml::ajax();

and you setup url as array

string,string, array,array

ajaxSubmitButton($label,$url,$ajaxOptions=array(),$htmlOptions=array())

Thanks! That explains much! As I wrote, I want this AJAX request to be fired when page finishes loading. With a little help from the other post, I mentioned and with a lot of help from you (thanks!) I was able to mange this with such code:


<script type="text/javascript">

    	$(function()

    	{

            	<?php

                    	echo(CHtml::ajax(array

                    	(

                            	'url'=>array('zlecenia/ajaxgetparams'),

                            	'update'=>'#section_2_div',

                    	)));

            	?>

    	});

</script>

And it finally works like a charm! :]

Although this is a bit off-topic, can you give me any tip, how to debug AJAX requests in firebug? I have it installed but I am as new to it as to AJAX request and up to now I was using it only to analyse HTML / CSS code of various elements of various pages. Don’t know how to debug JS, jQuery or AJAX calls in it.

Thanks, but I know that. This was put there only to debug and after I get this stuff working with your help, I removed any messaging from this code.


<script type="text/javascript">

    	$(function()

    	{

            	<?php

                    	echo(CHtml::ajax(array

                    	(

                            	'url'=>array('zlecenia/ajaxgetparams'),

                            	'update'=>'#section_2_div',

                    	)));

            	?>

    	});

</script>

This is same as




echo CHtml::ajaxSubmitButton

(

       'Submit',

       'url'=>'zlecenia/ajaxgetparams'

        array(

        'update'=>'#section_2_div',  

        )

);




EDIT: but only difference is that button is generated automatically

Igor,

It isn’t the same, because you are generating ajax button! I don’t want button, I want AJAX request to be fired automatically, when page finishes to load it contents. It’s a huge difference (at least for me), because your solution requires user reaction (click the button, ajax link or whatever), while mine doesn’t.

Cheers

I understand what you need i just explain you what is the difference, in your first post you been trying to use

ajaxsubmitbutton.

ajaxbutton,ajaxsubmitbutton are connected with clientchange.

Only Ajax is separated.

If you use example as i posted you, calls ajax with delegate() witch is global jquery id event function, if you set $live to false calls




Yii.CHtml.#'.$id,"jQuery('#$id').$event(function(){{$handler}});"



(and you cannot change it its bug)

I just have a solution for your said bug,

May be you were referring to non ability to set $live value as TRUE or FALSE on a component basis, for that I have the solution posted, its almost the same as changing $live=TRUE to $live=FALSE as default argument, Check it out. http://www.yiiframework.com/forum/index.php?/topic/14…175

Amit Singh :)

Thanks Amit for your post. No, my problem was rather based on that I never used AJAX in Yii and was lacking of some basic knowledge about it. Thanks again.