Ajax Processing



Since the URLs requested in the ZN Framework run a controller method, the code to be executed as an Ajax request must also be written to the controller method. Below is an example of using ajax with Jquery.

 

 

# Preparing the Appearance


Below is a sample view page containing the ajax code.

File: Views / Home / exampleAjaxProcess.wizard.php
<script>
$.ajax
({
    url     : "URL::site('Home/exampleAjaxProcess')",
    type    : "post",
    data    : {"name":"Ajax Process", "value":"Example Data"},
    dataType: "json",
    success : function(data)
    {
        alert(data.name);
        alert(data.value);
    }
});
</script>

Since the wizard page is used above , the parentheses are used instead of the php tags.

 

 

# Preparing your controller


Below is the content of the controller method where the request for ajax is shown for illustrative purposes.

File: Controllers / Home.php
<?php namespace Project\Controllers;

use Http, Post;

class Home
{
    public function exampleAjaxProcess()
    {
        Http::isAjax() or exit;

        # Your codes...

        echo json_encode(Post::all()); exit; # Return Values
    }
}

If your ajax content contains more HTML code than PHP code, you can create a view of your content and include it in the control :: import :: library.

<?php namespace Project\Controllers;

use Http, Post, Import;

class Home
{
    public function exampleAjaxProcess()
    {
        Http::isAjax() or exit;

        Import::view('Home/myAjaxContent'); exit;
    }
}

 

 

# Top Block Out


Set the value of ajaxCodeContinue in the following file to false if you are looking for an output that will cause any output on the startup controller . You can see this output in your ajax results, especially since Masterpage :: usage will cause top output.

File: ( ZN OE ) Config / Starting.php