Controller is firing but I can't get the $_GET - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Controller is firing but I can't get the $_GET (/showthread.php?tid=70292) Pages:
1
2
|
Controller is firing but I can't get the $_GET - richb201 - 03-21-2018 I am sending a request to my CI program from a remote javascript program running in a browser. The call on the javascript client is Code: var xhr = new XMLHttpRequest(); PHP Code: public function register() RE: Controller is firing but I can't get the $_GET - dave friend - 03-21-2018 If i read this documentation correctly the parameter for send is ignored for GET requests. You probably need to do a POST request. RE: Controller is firing but I can't get the $_GET - richb201 - 03-21-2018 Thanks Dave. The reason I am using GET is that I need to get data back from the server. The Browser sends the email address of the user and the server sends back a list of data for that user which I use to populate the popup in the browser. My understanding is that Post is a one way path from the browser to the server. RE: Controller is firing but I can't get the $_GET - jreklund - 03-21-2018 (03-21-2018, 08:55 AM)dave friend Wrote: If i read this documentation correctly the parameter for send is ignored for GET requests. You probably need to do a POST request. That's indeed correct. Or add the variable into the url, to get GET working. (03-21-2018, 09:37 AM)richb201 Wrote: My understanding is that Post is a one way path from the browser to the server. No, you can send back information with any method. Getting the data: https://www.codeigniter.com/user_guide/libraries/input.html#using-the-php-input-stream Send back JSON. https://www.codeigniter.com/user_guide/libraries/output.html#CI_Output::set_content_type RE: Controller is firing but I can't get the $_GET - dave friend - 03-21-2018 You can send data back from a POST too and it is done identically to the way you would respond to a GET. It's only AJAX after all. You'll want to add handling to the JavaScript. Code: document.onreadystatechange = function () { You can also use event listeners to handle readyState changes. Here's one for DONE a.k.a. xhr.readyState == 4 Code: xhr.addEventListener("loadend", callbackFn, false); Where "callbackFn" is a function to handle the response. It can be an in-line anonymous function too. And if you really, really, really insist on using get the way to pass data is to construct your url complete with a query string. Code: var url = 'http://localhost/Subit_backend/register' + '?email=' + jsonResponse["email"]; // if jsonResponse["email"] is a string RE: Controller is firing but I can't get the $_GET - richb201 - 03-24-2018 The document.onreadystatechange is not firing, although I do see the xhr.readyState go from 0 to 1. I am in a background script. Will that affect the use of statechange? document.onreadystatechange = function () RE: Controller is firing but I can't get the $_GET - richb201 - 03-24-2018 I managed to see the message being sent in the network debugger. It is: http://localhost/Subit_backend/register%7B%22lat%22:41.058304899999996,%22long%22:-74.0711244,%22email%22:%[email protected]%22%7D The status (from the network tab in the chrome debugger) is (pending). Is this because it was never recvd by Subit_backend? class Subit_backend extends CI_Controller { public function __construct() { parent::__construct(); $this->load->database(); $this->load->helper('url', 'input' ); } public function register() { if (isset($_GET["email"])) { $this->db->db_select("employees"); } } } RE: Controller is firing but I can't get the $_GET - jreklund - 03-25-2018 Just send it with POST instead. It's so much easier and your url don't get bloated. You are adding the following in the url: Code: {"lat":41.058304899999996,"long":-74.0711244,"email":"[email protected]"} But there are no ?email= so it dosen't know you want to pick it up with $_GET. http://localhost/Subit_backend/register?email=%7B%22lat%22:41.058304899999996,%22long%22:-74.0711244,%22email%22:%[email protected]%22%7D RE: Controller is firing but I can't get the $_GET - richb201 - 03-25-2018 I didn't realize that there needed to be an "=" to use $_GET in php. But as I was saying I am not even getting to the $_GET["email"] line in my controller. I am using JSON.stringify of my array which contains "lat", x, "long", y, "email", email_address. Perhaps that is the mistake? I can surely use POST instead of GET if nothing else needs to change in the xhr.open(). I took a look at the network messages and it seems that the message from the client is OK (except for the lack of "=" !) but it is stuck in status = pending which I think is a problem with the server and the way I have set up the debugger in phpStorm. I put in a support request from them but it could easily be a week before they respond. In the meantime I will try POST to see if it changes things. I also tried switching to just sending "email=". This is the header using =
RE: Controller is firing but I can't get the $_GET - jreklund - 03-25-2018 If you wan't to send pure Json with XMLHttpRequest*, this is how you do it. You will need to add readyState etc yourself. * With POST. GET are just there so you can look at something that works. But that's not recommended. Code: <html> PHP Code: <?php |