XF 1.5 Ajax call from template

Hello, i want to make an ajax call from my template in module.

I have created custom page in settings this way:
XML:
<option option_id="myPage" edit_format="callback" data_type="array" can_backup="1">

<edit_format_params>myClass:outputFunc</edit_format_params>

</option>

PHP:
PHP:
class myClass {

public static function outputFunc($content, $params, $template){

$ret_val = '

            <div style="text-align: center;">

                <input type="submit" class="button primary" id ="ajaxButton" value="ajaxButton" />

            </div>

            <script>   

                $("#ajaxButton").click(function(e) {
                  e.preventDefault();
                  var data = { "test": "yes"};

                    $.ajax({

                        type: "POST",

                        url: location.href,

                        data: data,

                        success: function(result) {

                            alert("ok");

                        },

                        error: function(result) {

                            alert("error");

                        }

                    });

            });           

            </script>

';

return $ret_val;

}

}

Now i need to do some php actions on ajax call
PHP:
if (isset($_POST['test']) && $_POST['test'] == 'yes')

// TODO

Where should i place it? It doesn't work in outputFunc function. Should i need to create an external script for this?
 
Firstly, use xenforo ajax function to make requests:
XenForo.ajax(url, params, callback)

Secondly, don't use html/js in php scripts. You can put your html/js in template.
 
Top Bottom