JQuery AJAX syntax

var data="abc";
$.ajax({
    type: "POST",
    url: "page.php",
    data: {
        "data":data,
    },
    dataType: "json",

    //if received a response from the server
    success: function( datas, textStatus, jqXHR) {

    },

    //If there was no resonse from the server
    error: function(jqXHR, textStatus, errorThrown){

    },

    //capture the request before it was sent to server
    beforeSend: function(jqXHR, settings){

    },

    //this is called after the response or error functions are finished
    //so that we can take some action
    complete: function(jqXHR, textStatus){

    }

}); 

Ask Confirmation Before Call an Ajax request in Jquery

# HTML Button::

<button onclick="status_change(1)" >Call Ajax</button>

# JavaScript Code::

<script type="text/javascript">

function status_change(id) {
    $.confirm({
       'title' : 'Attention!',
       'message' : 'Want to Continue?',
       'buttons' : {
            'YES' : {
            'class' : 'blue',
            'action': function(){
                $('#loading').html('ring.gif'); 
                $.ajax({
                   type: "POST",
                   url: "url/"+id,
                   data:{ID:id},
                   success: function(msg){

                            window.location.reload();
                   }
               });
            }
         },
         'NO' : {
            'class' : 'gray',
            'action': function(){
                 return false;
            } 
              // Nothing to do in this case. You can as well omit the   action property.
       }
    }
 });

}
</script>

How to render view in a variable inside controller in cakephp?

How to Render view in a variable inside Controller in CakePhp?

By using this code, you can fetch the html template content in a variable as a string.

You can also pass the parameters if required.

// Create an Instance of view
$view = new View($this, false);

// Passing the Needed Parameters

$view->set(compact('params1', $params1));
$view->set(compact('params2', $params2));


// Now load the template content as a string in variable,

$html = $view->render('ajax_product_template');

Check for Ajax Request in CodeIgniter

Check for Ajax Request in CodeIgniter:

The CodeIgniter Input Class Provides lots of  helper functions to deal with user inputs , security etc.this class is initialized automatically by the system so there is no need to do it manually.

I always recommend you to use this class instead using PHP Core Input Variables while dealing with forms and requests.

To check Ajax Request in CodeIgniter, we will use codeIgniter Output Class Method class is_ajax_request() , this function checks to see if the HTTP_X_REQUESTED_WITH server header has been set, if set response will be true otherwise false means it will returns a boolean response.

 

we can use

 $this->input->is_ajax_request()

from the input class:

if (!$this->input->is_ajax_request()) {
     exit('No direct script access allowed');
}

As, CodeIgniter Library::

is_ajax_request()

Returns:TRUE if it is an Ajax request, FALSE if not
Return type:bool

Checks to see if the HTTP_X_REQUESTED_WITH server header has been set, and returns boolean TRUE if it is or FALSE if not.

 

 

Source:
https://www.codeigniter.com/user_guide/libraries/input.html