Database query by ajax/jquery

Hi all,

I am trying to insert data into my DB, I will need to get this data later on an other page to display but for now I just need it in the DB.

I am having Trouble working out how to get the data in to the function.

Does the code need to be in the Page controller or can I have a different controller for all my ajax methods? (this would be better).

(The page is within a tab too)

I have tried this way.


    $('#homeSave').click(function(){

        //alert('hey this works');

        $.ajax({

            async:true,

            cache:false,

            dataType:'json',

            type:'post',

            data:{

                box1:$('#homeBox1').val(),

                box2:$('#homeBox2').val(),

                box3:$('#homeBox3').val(),

                box4:$('#homeBox4').val()

            },

            url: "/index.php?r=texHome",

            error: function(XMLHttpRequest, textStatus, errorThrown){

                dialogObj.dialog('destroy');

                dialogObj.attr('innerHTML','<p>There was a problem saving the information</p>');

                dialogObj.dialog({

                    modal:true,

                    title:'Attention',


                    buttons:{

                        'Ok':function(){

                            dialogObj.dialog('destroy');

                        }

                    }

                });

            },

            beforeSend: function(){

                dialogObj.dialog('destroy');

                dialogObj.attr('innerHTML','<p>Saving...</p>');

                dialogObj.dialog({

                    modal:true,

                    title:'Please Wait'

                });


            },

            success: function(data) {

                dialogObj.dialog('destroy');

                dialogObj.attr('innerHTML','<p>Completed</p>');

                dialogObj.dialog({

                    modal:true,

                    title:'Absences updated',

                    buttons:{

                        'Ok':function(){

                            dialogObj.dialog('destroy');

                            //$('home_dialog_box').val('hello');

                            $('#tabs-main').tabs('load',$('#tabs-main').tabs('option', 'selected'));


                        }

                    }

                });

            }

        });

    });

I have also tried this way


echo CHtml::ajaxSubmitButton(

  'Save',

  array('/edithome/texHome') 

);

Both ways I can not seem to get to work.

Can someone please help going thought the process step by step on how I:

Call a function.

Pass the vars I need to the function.

After I have done all my stuff

return data from the function.

Thanks

Lets discuss lite bit about javascript and ajax, ajax calls and javascript is not server side script.

what you trying to do, what i understand in this example is to call a link witch will call a ajax call , witch seems to me is not possible , javascript have to be in your layout, or view (implemented as part of a web browser) ,than on event in this case click you have to call a controller action, or extended Caction witch will do a db job, because as i said javascript is not server side script.

Hi,

As I understood you want to call a Yii script ("/index.php?r=texHome",) and when successful display a dialog box saying Completed right?

Well, on the server side, the best advice is to create a view and then on your controller (the one YOU DONT SPECIFY on the url -textHome what it is? do you have a rule for that -if that is so then I assume that textHome is your action, where is your controller name? goes to a specific controller?)

Maybe with what I told you above you found the problem but I will like to add something else to your code. On your success function there is a problem, you actually do not know if the server side was successful or not. You just assume is ok and sometimes the PHP code compiles good but then something happens without error and your transactions are not executed. You should always check the value BEFORE SUBMISSION, ON SUBMISSION -SERVER, ON RECEPTION -CLIENT.

Cheers

Yes this I know, the Javascript (ajax,jquery)will call a php page on the server which has the function in it.

I need a way of calling different functions from within the same php file in yiiframeworks.

This function in the php page that is to be called should do the DB insert/get and return true for insert, database record(s) for get.

I know this can be done as I have done it with normal php pages but I am finding it hard to do this within the yiiframework.

Can you help me / explain how this can be done or tell he another way of doing this.

The code about is in the view. I would like this code to call say (ajax_requests.php (server side)) functions insertText and GetText and return true for insertText and a recordset for GetText.

Should ajax_request be a model, controller or view/controller?

How do I call ajax_request -> getText()

How do I pass vars into ajax_request -> getText().

When I have a client script that calls a server, it always calls a valid Yii url, and that goes (if not defined differently on the rules) by a CONTROLLER/ACTION. That means that you receive a REQUEST from the client and the action RESPONDS accordingly. Its RESPONSE is in a VIEW (if you do not do easy JSON RESPONSES though, which I think is silly to have a VIEW even though is a good MVC practice)

ajax_request controller / getText action is called by creating a url like this on the view where you render the above code:





<?php echo $this->createUrl('ajax_request/getText');%>




How do you pass variables? Well if you are doing ajax calls via JQuery you just need to place them on the data attribute of the ajax call.





 $.ajax({

            async:true,

            cache:false,

            dataType:'json',

            type:'post',

           data: .... parameters here!

...



Thanks I will try this later and let you know how I get along.

Thanks this worked great,