In a panel field I can post data with a route with $_GET
but can’t get it to work with $_POST
.
Get JS that work
$.fn.ajax = function(fieldname, data) {
var blueprint_key = $('[data-field="' + fieldname + '"]').find('[name]').attr('name');
var base_url = window.location.href.replace('/edit', '/field') + '/' + blueprint_key + '/' + fieldname + '/ajax/';
var selector = '.field-name-' + blueprint_key;
$.ajax({
url: base_url + 'test/test',
type: 'GET',
success: function(result) {
var obj = $(selector + ' iframe').attr('srcdoc', result + Date.now());
}
});
};
Post JS that does not work
$.fn.ajax = function(fieldname, data) {
var blueprint_key = $('[data-field="' + fieldname + '"]').find('[name]').attr('name');
var base_url = window.location.href.replace('/edit', '/field') + '/' + blueprint_key + '/' + fieldname + '/ajax/';
var selector = '.field-name-' + blueprint_key;
$.ajax({
url: base_url,
type: 'POST',
data: data,
success: function(result) {
var obj = $(selector + ' iframe').attr('srcdoc', result + Date.now());
}
});
};
Get php route that work
public function routes() {
return array(
array(
'pattern' => 'ajax/(:any)/(:any)',
'method' => 'get',
'action' => function($var1, $var2) {
$html = '';
$html .= tpl::load( __DIR__ . DS . 'template-site.php', $data = array(
'field' => $this,
'page' => $this->page()
));
return $html;
}
)
);
}
Post php route that does not work
public function routes() {
return array(
array(
'pattern' => 'ajax',
'method' => 'POST',
'action' => function() {
$html = '';
$html .= tpl::load( __DIR__ . DS . 'template-site.php', $data = array(
'field' => $this,
'page' => $this->page()
));
return $html;
}
)
);
}
Fatal error: Call to a member function direction() on a non-object in C:\wamp\www\splitfield\panel\app\src\panel.php on line 362
I don’t know if my PHP is wrong or if it’s the JS.
UPDATE
Here is the plugin:
In blueprint:
fields:
test:
type: splitfield
It does not work, so it’s for testing purposes only.
Update 2
I can set this in the route:
'method' => 'GET|POST',
And this in my ajax template:
print_r($_POST);
But then again I get the same error as before:
Fatal error: Call to a member function direction() on a non-object in C:\wamp\www\splitfield\panel\app\src\panel.php on line 362