Is it possible to call a php class function DIRECTLY using ajax?
Something like below... except ajax...
myclass::myfunction();
I've been using the jquery library to work with AJAX.
$.get('control.php', {func: funcName, arg1: arg1});
The above is similar to what I'm trying to achieve MINUS the control.php;
I'm not sure if this is even possible, but I just thought it would be nice to skip the landing page (control.php) that recieves the funcName. I have a bunch of conditional statements that sort out what class function to run based on the funcName recieved.
It seems kind of silly to do this, to have a separate page just to handle function calls.
Is there a better way?
No.
If this were possible, it would be a gaping security hole.
No. You can't invoke a method directly that way.
You could use routing (like the technique used in CodeIgniter and CakePHP) but that's just syntactic sugar that does the same thing -- control your routes to actions.
It is not possible because of a simple reason. How should the AJAX knows, where to find the function. It needs to have a URL to locate the function so it doesn't work without a php file in between.
No for security reasons but there is no reason why you can't do something like this
function run($args){
//do stuff
}
echo run($_REQUEST);
//or
echo run($REQUEST['name']);
Related
I do one request and my URL has a parameter like this .../index.php?customer=abc
In index.php's class $_GET['customer'] is available.
There are multiple other classes being created then.
Finally in somefile.php containing some different class someClass, $_GET['customer'] is no more available.
I am forced to use a framework that uses a form that eval()s PHP code on button click.
new TDynButton($body, "login", ... , "\$this->win->doLogin();");
IndoLogin() there is no $_GET['customer']. Cannot understand why. Is it possible if this framework uses action=GET in the background that I am losing my $_GET? Im totally lost.
Thanks.
My approach would be to give the information in $_GET['customer'] to the instatiated object by passing it in the constructor and store it in a private member. This way you have the information needed and no direct access to $_GET is nessessary. This is anyway a better design I think.
$_GET is a global variable that will be available throughout the script you use it in. You have to pass it to the script, though - such as somefile.php?customer=peter
Yes, $_GET is a superglobal variable that is available in all PHP scripts.
And yes, generally, framework convert/sanitaze the GET/POST arrays and clears them.
I'm trying to get a list of functions that have already run in the context of an operation. And that's where the need arised.
Is there a way to get something like this work?
function funcX()
{
echo this.nameOrSomethingHere; //outputs funcX
}
As this example demonstrates, it is possible to achieve this functionality in Javascript.
I can always go for an alternative solution for taking care of my original need like with an alternative way around as follows;
function funcX()
{
$this_function_name = "funcX";
Add_this_to_the_already_executed_functions_list($this_function_name);
}
And here, the Add_this_to_the_already_executed_functions_list functions job is to take the passed string and add it to a globally defined array so at the end of the shutdown process you can get a view of all the functions that have been run in the last page.
The above method would work, but, obviously, it's not elegant cause it's not dynamic.
It would have been nice to be able to do something like this
function funcX()
{
Add_this_to_the_already_executed_functions_list(this.????);
}
The question is if there is a way to do this in PHP?
You can use __FUNCTION__ to get the current function's name.
http://us2.php.net/manual/en/function.debug-backtrace.php
debug_backtrace will tell you that and a lot more. best for debugging, but you could use it for whatever you wanted.
Use __FUNCTION__
See 'magic constants'
I know this is a really simple thing that I really should know but I'm trying to learn cakephp without having done much php before. I've been told thats a stupid idea, but I'm doing it for fun and so I'm doing it.
I want to pass an array from one controller action to another controllers action and then pass it to the view. I have:
sponges_controller.php
$info = $this->data;
$this->redirect(array('controller'=>'baths', 'action'=>'dashboard', $info));
baths_controller.php
function dashboard($info) {
$this->set('info', $info);
}
and then
<?php echo debug($info); ?>
in the view for dashboard.
I've tried various ways but can't make it work. All it does is print out Array()
Plz help me! :) Julia
You can't pass data that way from one controller to the other as far as I know, at most you can concat a string to the action, like an ID for view or editing.
If you want to pass the info you could try setting it in the SESSION variable in the following way:
$this->Session->write('Info', $info);
And in your other controller you can check for it:
$this->Session->read('Info');
It looks like cake will not let you pass an array into a controller action. I set up a simple example and I got an 'array to string conversion error'. Is there a specific reason why you aren't just posting the data to baths/dashboard? I can think of a workaround for your problem, but it is quite messy.
8vius's solution above will definitely work.
Here is another way, but using sessions is probably a lot better
$str = http_build_query($info);
$this->redirect('/baths/dashboard?'.$str);
So then in your baths/dashboard action, you will have access to your data using the php $_GET array.
So if you originally had this->data['name'] you can access it with $_GET['name']
I'm not sure about the passing data in different controllers but within the same controller we can do it just like a function call by writing something like this.
$this->function_name($info);
This will perfectly work as intended. I've not tried this type of data passing in different controllers function.
Kohana (and probably other frameworks) allow you get a route and echo its URL, creating routes that are easy to maintain.
Contact
Is this OK to have in the view, or should I assign it to a variable, and then pass the view the variable?
Thanks
You aren't performing logic here. This is perfectly acceptable.
Of course your view code would be a bit cleaner if you created a variable in your controller, but this really is fine IMHO.
I find such a concatenation unnecessary. It seems url::base() going to be used in every link on the site. Why not to have a method to add it automatically? Something like Route::url("contact")
And usage of such a construct in the template is OK.
You can create a function or static method for generating urls:
public static function url($routename, array $params = NULL)
{
return url::base().Route::get($routename)->uri($params);
}
I'm using GWT to dynamically load html snippets from php script. I define the snippet i want the php script to return in the url (test.php?snippet=1). Now in GWT i have a function "getSnippet(int snippet id)" that uses a RequestBuilder to retrieve the snippet. It works perfectly fine, but it bothers me that i have to create a new RequestBuilder everytime getSnippet gets called. I'd rather have one ReqestBuilder and just change the url when getSnippet is called...
Is there a way to do this ?
Thank you !
In looking at the source code, I can't see a good reason why they are doing this. I would like to think that the GWT developers decided to leave out the setUrl method for a reason and included it in the constructor instead.
If you really want to do it, one way around this would be to extend the class and add a setUrl(String url) method. Modify all your current uses of RequestBuilder to use your newly extended class and see if anything breaks.