Jump to content

inputfield ajax


Recommended Posts

I'm trying to call an ajax function for an inputfield using Soma's method below by intercepting the call in the module init function but for some reason $this->input is always NULL.

 

public function init() {
		parent::init();
		$dir = dirname(__FILE__);
		require_once("$dir/Location.php");
		require_once("$dir/LocationArray.php");

		$this->wire('config')->js('InputfieldLocations', array(
			'url' => "/admin/locationmodule/ajax"
		));

		if ($this->config->ajax && $this->input->ajaxfunction == 'getcities') {
			header('Content-Type: application/json');
			echo $this->get_cities();
			exit;
		}
	}

 

Link to comment
Share on other sites

$this->input->post is not populated for AJAX requests (caused by PHP, see this). I use something like this.

<?php
$raw = file_get_contents('php://input');

// optionally check for Content-Type headers
if(is_array($parsed = json_decode($raw, true))) {
    $this->wire->input->post->setArray($parsed);
    $this->config->ajax = true;
}

// use $this->input->post->varName as usual

 

Edited by abdus
added stackoverflow link
  • Like 2
Link to comment
Share on other sites

@neonwired when I use 

$.ajax({
    url: '/endpoint/',
    type: 'GET',
    data: {
        id: 4,
        ajaxfunction: 'someaction'
    },
    success: function(data) {
        console.log(data);
    }
})

where endpoint just echoes the request back

<?php
header('Content-Type: application/json');
echo json_encode(input()->get->ajaxfunction);
return;

and it works (I get my request back, and can access $input->get->ajaxfunction just fine)

Long shot, but can you try using capital GET as ajax type (instead of get)?

Link to comment
Share on other sites

3 hours ago, abdus said:

$this->input->post is not populated for AJAX requests

That's not correct. It's not populated by json data – which by the way is not really a standard or alike – but only by data formatted in application/x-www-form-urlencoded or multipart/form-data. Both is what a <form> can send, but ajax can also send data in that format. It's just that ajax libs nowadays favor json (, it's not like it's name would include xml :D).

  • Like 1
Link to comment
Share on other sites

2 minutes ago, LostKobrakai said:

only by data formatted in application/x-www-form-urlencoded or multipart/form-data

You're absolutely correct. If the request (AJAX or plain HTTP) is made with a query string (x-www-form-urlencoded) or file input (multipart/form-data) $_POST is populated as usual, but not when it's anything else.

http://php.net/manual/en/reserved.variables.post.php

Link to comment
Share on other sites

Do you have redirects? Check the developer console on your browser for double requests and 301 responses.

If you have redirects, make sure you're specifying exact URL of the endpoint (with or without trailing slash) 

Link to comment
Share on other sites

20 hours ago, abdus said:

Do you have redirects? Check the developer console on your browser for double requests and 301 responses.

If you have redirects, make sure you're specifying exact URL of the endpoint (with or without trailing slash) 

Doesn't look like there's any redirects. $_POST is fine as well. It seems i can iterate through it or call a variable directly but if i dump or print_r $input->post() i get an empty object. Weird.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...