Pass $_GET variable through ajax call's php file - ajax

I have an jQuery AJAX call that lists records from database based on $_GET variable called "website_identificator". Imagine the main file is called "index.php" (where the AJAX call is presented) and the PHP script file that is called by the AJAX is called "phpscript.php". Now also imagine that I'm using the $_GET "website_identificator" to decide what to show on this page, but this $_GET "website_identificator" is beneficial for the PHP script file so it can filter the records and show the right ones. How to I use this $_GET variable in "phpscript.php", because when I try to use it like this:
<?php $getVar = $_GET['website_identificator']; ?>
PHP understands it like "phpscript.php?website_identificator=STRING" but not "index.php?website_identificator=STRING" despite the fact that I called the file through AJAX call.

I understand that you call index.php with the query parameter website_identificator? You have to include that value in the AJAX request and pass it on to phpscript.php. There is no magical way to retrieve the query parameters from another script, both are standalone.

Related

Laravel Routes - Possible to reuse ajax route call from one form on different form?

I have an existing route I created to perform an ajax call for a chained select:
Route::get('add/{id}','AddAssetController#getModel');
The above queries the db for models belonging to each manufacturer.
The url part of the ajax call (man_ID is the id of the manufacturer from the select):
url: 'add/' + man_ID,
The above works perfectly on the main form where I am using it. However, I've found that I need this chained select on more than one form. Is there a way to be able to use the same route for two different pages? I've tried calling it from a different page and in the console, I'm getting a 404 error.
Am I missing something? Is this possible?
You are probably calling the script from the differently nested pages, thus add/ is ultimately calling different path.
I would strongly advise you name your routes, and generate links like that:
Route::get('add/{id}','AddAssetController#getModel')->name('get-model');
and in your blade file
url: '{{route('get-model', ['id' => $id])}}',
if you still set on using paths to call your script, use them relative to your root:
url: '/add/' + man_ID,

Whats the difference between redirect and this in Codeigniter?

I am new in Codeigniter and it's one of the good frameworks of php. But on some conditions I'm confused. Like this one. If any of you have any clarification about my dough, it's a great help for me.
Offcouse redirects refresh the page and $this not but apart from this I want to know - anyhow both of them used to go to somewhere else on view pages or like in other controller or in same controller to other methods.
But we don't use these side by side because when getting any of them it will go to that page or method without checking the next lines.
In case of a normal difference then have lot's of but I just want to know about the condition of going to next page or method when we use redirect or $this like this -
$this->Function($value); //It's method of same controller.
redirect('Controller/function'); //It's also doing same with page reload.
Thank for looking my problem.
Redirect()
When you will call any function of helper in codeigniter then you can call function directly without using any object. Helper in Codeigniter is collection of functions.
Redirect() method is a part of URL helper in Codeigniter.
For your ref. https://www.codeigniter.com/user_guide/helpers/url_helper.html
So, just load helper using $this->load->helper('url'); or you can also mention in autoload.php file.
$this->Function(); used to call a function from same controller
$this->Function(); used to call a function from same controller
redirect()
While building a web application, we often need to redirect the user from one page to another page. CodeIgniter makes this job easy for us. The redirect() function is used for this purpose.
redirect($uri = '', $method = 'auto', $code = NULL)
The first argument can have two types of URI. We can pass full site URL or URI segments to the controller you want to direct.
The second optional parameter can have any of the three values from auto, location or refresh. The default is auto.
The third optional parameter is only available with location redirects and it allows you to send specific HTTP response code.
Redirect means jumping to another function mentioned in the redirect method.
$this->Function($value); => jumping to another function and you can execute the code of the same function as well as pass the value back by returning value.
When you send request to codeigniter generally CI controller gets called and then function which is mentioned in uri segment. like below... So this will be another request.
redirect('Controller/function'); //It's also doing same with page reload.
But when you have to call another function within the same request then you can use below approach
$this->Function($value); //It's method of same controller.
This will execute the given function and return the value within same request.

How to custom API URL on codeigniter

I have a PHP CodeIgniter Controller with name index and have a method that get details of id kode ($kode) using API get method.
Now when i need to show kode data for example for id AALI
I call this URL
http://www.example.com/?q=AALI
My target
How to make user data accessible by next URLs
http://www.example.com/AALI
I've try using function _remap on code Igniter, but it still wont work.
Have a look at Codeigniter URLs
As per your statement, your controller name is index and there would be an index function in your controller which renders the default view. it means you have changed default_controller to index in your Config.php
Now if you read the link above about Codeigniter URLs, there is a way to get data which passed in the URL after "/" You have to load url helper you can either autoload(recommended) it or load it in your constructor or Controller as per your convenience
Then you can just type
$param=$this->uri->segment(2); // in case your URL is http://www.example.com/AALI
The first segment is controller itself, The second is the function if your url is complete and the third is the parameter in CI URL structure but if you are not providing function name the first segment will always be your controller . So the second is your parameter. Just save it in a variable and do what you like.

Use '#' with method in codeigniter like www.mydomain.com/controller#method

Is it possible to call a method of any controller like this:
www.mydomain.com/controller#method
instead of
www.mydomain.com/controller/method
#method(hash) values not detected by PHP, even with $_SERVER["REQUEST_URI"],
So It's not possible. You can detect the hash value with javascript alert(window.location.hash); OR Use ajax to prevent page refresh.

failed to return AJAX call when use_session_sid

I am trying to use php's use_trans_sid, so I will have phpsessid in all urls.
But, when I set use_trans_sid to 1, AJAX call did not get result properly.
Somehow the result truncated.
When I set use_trans_sid back to 0, AJAX call get result properly again.
What would be the problem?
I am using the Yii framework.
Check ajax url requests when use_trans_sid=1, if they haven't phpsessid var than you need manually add it to request url as GET parameter.
use_trans_sid=1 rewrites only page url, not js and ajax. When it using, PHP try to find session id in get parameter, if not found - created new session.
When you using use_trans_sid=0 ajax works, because PHP work over cookie session id
I have set php's use_trans_sid to 1.
With that, PHP will insert code to propagate session id.
In my case, PHP inserted a hidden variable in a form which has been encoded into json object.
As the result, ajax call get it as a request error (it did not get a json object).
I confirmed this by replacing the form with string like 'Hello'.
With that, PHP did not insert code to propagate session id. And ajax call get it as a good json object.
Now the problem has changed into 'how to make such interruption from PHP will not interfere ajax call?'

Resources