Loading


Advertisement:

Author Topic: Using Ajax call from within theme without breaking Osclass helpers?  (Read 810 times)

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2370
Hi there,

I am unable to get my head around this, somehow all Osclass helper functions are lost in space when I call a theme ajax php file which I would like to do some simple data manipulation.

What I have setup: in the theme file 'user-items.php' I added the following javascript to enable Ajax:
Code: [Select]
<script>
function DoAjax(id) {
var dataString = 'id='+ id;
$("#"+id).fadeOut(300);
$.ajax({
type: "POST",
url: "<?php echo osc_current_web_theme_url('ajaxAction.php'); ?>",
data: dataString,
cache: false,
success: function(html) {
$("#"+id).html(html);
$("#"+id).fadeIn(300);
}
});
}
</script>

Then in the theme file 'loop-single.php' I call the above javascript function to perform an ajax call. It all works fine, the file ''ajaxAction.php' does actually gets called and I am able to 'return' a value without having to do a complete page refresh (which was the whole goal of this excercise) BUT I am unable to use Osclass functions like Params::getParam(''); for example or any other Osclass helper function within this ''ajaxReserve.php' file when it's processed. :(

Is this normal behaviour, do I need to set some extra references (includes?) or should I just do something totally different?

Any help here / directions would be much appreciated.


Regards,
Eric

Liath

  • Hero Member
  • *****
  • Posts: 895
  • #Kurt_Cobain { margin-bottom: -6.5ft; }
Re: Using Ajax call from within theme without breaking Osclass helpers?
« Reply #1 on: February 27, 2017, 10:33:32 pm »
to use all osclass functions in your file, use:


Code: [Select]
require_once(ABS_PATH.'/oc-load.php');

at first line after your opening php tag

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2370
Re: Using Ajax call from within theme without breaking Osclass helpers?
« Reply #2 on: February 27, 2017, 11:40:31 pm »
Hi Liath,

thanks for the tip but no luck yet, somehow it doesn't work and I did do refresh page etc. but that line of code is throwing an error somehow also 'breaking' the echo return value.

I AM able to pick up the parameter in ajax php file using '$var = $_REQUEST["id"];'

But as soon as I am trying the '$var = Params::getParam('id')' it is not working.
If I use the code you suggested on first line the whole thing seems to break and other code after is no longer working.


Regards,
Eric

Liath

  • Hero Member
  • *****
  • Posts: 895
  • #Kurt_Cobain { margin-bottom: -6.5ft; }
Re: Using Ajax call from within theme without breaking Osclass helpers?
« Reply #3 on: February 27, 2017, 11:45:16 pm »
sure...
$_REQUEST["id"] is a server variable and mostly always available
Params::getParam('id')' is an OSClass function and only available when neccessary classes are loaded

which error is thrown after adding my code?

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2370
Re: Using Ajax call from within theme without breaking Osclass helpers?
« Reply #4 on: February 27, 2017, 11:51:24 pm »
Ahum, should have looked for error in my server php-fpm server log sooner.
The error is: PHP Warning:  require_once(ABS_PATH/oc-load.php): failed to open stream: No such file or directory in theme dir

So, the ABS_Path is not correct, let me see what happens if I fix that.

Thanks!

Liath

  • Hero Member
  • *****
  • Posts: 895
  • #Kurt_Cobain { margin-bottom: -6.5ft; }
Re: Using Ajax call from within theme without breaking Osclass helpers?
« Reply #5 on: February 27, 2017, 11:57:32 pm »
ok :)

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2370
Re: Using Ajax call from within theme without breaking Osclass helpers?
« Reply #6 on: February 28, 2017, 12:05:22 am »
Okay, seems it works now, I had to replace the ABS_PATH variable with my full server path (on Apache) using:
/var/www/domain/site folder/oc-load.php

Thanks Liath, I had tried to include the oc-load earlier without luck so your reply confirmed I was on the right track and with some debugging the fix is here :D

Regards,
Eric

Liath

  • Hero Member
  • *****
  • Posts: 895
  • #Kurt_Cobain { margin-bottom: -6.5ft; }
Re: Using Ajax call from within theme without breaking Osclass helpers?
« Reply #7 on: February 28, 2017, 12:07:19 am »
good that you solved it :)

and now, i know why it doesnt work earlier...  ::)  the ABS_PATH isnt available before including oc-load.php  ;D




sorry, my mistake
« Last Edit: February 28, 2017, 12:08:59 am by Liath »

Web-Media

  • Sr. Member
  • ****
  • Posts: 323
  • Web
Re: Using Ajax call from within theme without breaking Osclass helpers?
« Reply #8 on: February 28, 2017, 10:08:42 am »
 Do Use osc_ajax_url (file) not current_theme_file (file) .

I'm  mobile , not able to offer more details . :)

dev101

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 1691
  • osclass.work
Re: Using Ajax call from within theme without breaking Osclass helpers?
« Reply #9 on: February 28, 2017, 04:33:37 pm »
Another idea is to create and register theme page with name "page-ajax.php", then you will be able to call anything inside it straight away without re-including core files (which may be a performance benefit, as well). And, more secure, as you can prevent being accessed/called directly.

@ Web-Media
There is no such helper, maybe you were referring to osc_ajax_plugin_url($file) ?

Regards

Web-Media

  • Sr. Member
  • ****
  • Posts: 323
  • Web
Re: Using Ajax call from within theme without breaking Osclass helpers?
« Reply #10 on: February 28, 2017, 09:28:19 pm »
osc_ajax_plugin_url . My mistake .. only works with plugins .
But .. you can install a plugin also on  theme  activation and use  the helper and ajax from plugin file

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2370
Re: Using Ajax call from within theme without breaking Osclass helpers?
« Reply #11 on: March 01, 2017, 11:21:36 am »
Hi Web-Media,

I didn't want to make a plugin for this, it's a personal customization (added db field in t_item manually) in Osclass and theme since creating a plugin for what I want would simply mean too much overkill. I ended up with using the oc-load and a 'hard-coded' full path. I know there's a way to make the path dynamicly but I am just lazy in this case ;)

I am kinda disappointed I need to include the oc-load thinking that was already loaded in memory and simply available but I guess here's some basic knowledge missing on my side with regard to web development :)

osc_ajax_plugin_url . My mistake .. only works with plugins .
But .. you can install a plugin also on  theme  activation and use  the helper and ajax from plugin file

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2370
Re: Using Ajax call from within theme without breaking Osclass helpers?
« Reply #12 on: March 01, 2017, 11:31:28 am »
Hi dev101,

that sounds like the best solution but I need more help with this.
For starters, would that "page-ajax.php" be the substitute for my "ajaxAction.php" ie. the page that is called and used to perform action(s)? And if so, why specificly that name, is it a 'default' page name used by Osclass?

The other thing is, I don't know how to do the registration of that theme page but I do want to make the change you suggest and get rid of another core file load action.


Thanks,
Eric

Another idea is to create and register theme page with name "page-ajax.php", then you will be able to call anything inside it straight away without re-including core files (which may be a performance benefit, as well). And, more secure, as you can prevent being accessed/called directly.

@ Web-Media
There is no such helper, maybe you were referring to osc_ajax_plugin_url($file) ?

Regards

dev101

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 1691
  • osclass.work
Re: Using Ajax call from within theme without breaking Osclass helpers?
« Reply #13 on: March 01, 2017, 07:19:14 pm »
It is the pattern Osclass internally looks for, so yes, the name is important unfortunately.
Go to Admin Dash and simply create that page manually ;)

Regards

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2370
Re: Using Ajax call from within theme without breaking Osclass helpers?
« Reply #14 on: March 01, 2017, 09:24:38 pm »
So if I understand correct you're telling me to create a new 'Page' in Admin, give it the 'internal name' of 'page-ajax.php' ??

It is the pattern Osclass internally looks for, so yes, the name is important unfortunately.
Go to Admin Dash and simply create that page manually ;)

Regards