Open Source & Stuff  
  |     Main Site     |      Forum     |     Contact     |
Ajax Linked Select boxes :  forum.Salix.gr forum.Salix.gr
Comments, questions, usage, bugs. 
Goto Thread: PreviousNext
Goto: Forum ListMessage ListNew TopicSearchLog In
messageAjax Linked Select boxes and Wordpress
Posted by: gramps (IP Logged)
Date: October 27, 2007 10:01PM

I was lucky to stumble across your great script after several days of looking for something like this.
Your example worked great straight 'out of the box', and now I'm trying to incorporate it into a WordPress plugin for use within the wp-admin area. So far, I can't get it to work.

1) I created a folder under wp-content/plugins, into which I placed: sc_select_ajax_handler.php, sc_classes, top_file.php and prototype.js
2) The only changes I have made to those files are to change the database connect details (tested OK with your example.php file and data outside of WordPress, calling the database connection details from an included config file).
3) The function which creates my admin page for the plugin simply uses the content of example.php.
4) The plugin activates without error, and the database seems to connect OK using only the connection established within WordPress (I get a row count of 4, 3, 1 and 1 for the four select boxes, respectively)

The problem is that nothing happens on change of any of the values.

I would really appreciate any thoughts, comments or suggestions you (or anyone else) might have - I'm really struggling with it as I'm new to AJAX and weak in javascript.

Again, thank you for generous contribution of this script.

messageRe: Ajax Linked Select boxes and Wordpress
Posted by: panos (IP Logged)
Date: October 28, 2007 02:11PM

Hi,

first of all, thanks for your kind words.

Now about your problem. I think it is the common problem ajax call cant find the file sc_select_ajax_handler.php by default it hits root folder, that means if you are at www.mydomain.com it hits http:/ / www.mydomain.com/sc_select_ajax_handler.php but in your case
it is located in http: / /www.mydomain.com/wp-content/plugins/plugin_name/sc_select_ajax_handler.php

There are 2 solutions,
a. copy sc_select_ajax_handler.php to root folder and correct include in that file
b. edit sc_classes.php and about line 196 you will find

var $php_ajax_handler = 'sc_select_ajax_handler.php';

note that this is javascript, change that to

var $php_ajax_handler = '/wp-content/plugins/plugin_name/sc_select_ajax_handler.php';

or to make plugin more "moveable" to something like

var $php_ajax_handler = '<?php echo $PLUGINS_FOLDER; ?>/plugin_name/sc_select_ajax_handler.php';

I there is some wordpress function or global var to get that path. If not just hard code the path.

That will fix it.

Best regards

Panos



Edited 1 time(s). Last edit at 10/28/2007 02:13PM by panos.

messageRe: Ajax Linked Select boxes and Wordpress
Posted by: gramps (IP Logged)
Date: October 29, 2007 09:04PM

Hello Panos


Thank you for your suggestions. I have tried implementing all of their permutations, and opted finally to edit sc_classes.php to keep all relevant files in one directory. I have also hard coded all 'includes', as well as the location of prototype.js, to their absolute address. Regrettably none of that seemed to solve the problem.

The problem must happen after the initial mysql queries, because when the page first loads the select boxes are populated correctly. Could the WordPress directory structure somehow be messing with the url passed on the triggering event?

I really don't know how you have time to deal with user issues such as this, but I really appreciate your thoughts.

Kind regards,

messageRe: Ajax Linked Select boxes and Wordpress
Posted by: gramps (IP Logged)
Date: October 30, 2007 12:57AM

Hello again

Since my last post, I have discovered that WordPress v2 includes SACK - a Simple AJAX Code Kit, and for AJAX admin plugins, a special url to which to send all AJAX requests. So please don't spend any further time trying to resolve the issues I've bumped into trying to integrate your script with WordPress.

Many thanks,

messageRe: Ajax Linked Select boxes and Wordpress
Posted by: panos (IP Logged)
Date: October 30, 2007 09:36AM

No worries my friend I am always happy helping... atleast try to smiling smiley

messageRe: Ajax Linked Select boxes and Wordpress
Posted by: gramps (IP Logged)
Date: November 01, 2007 02:03AM

Hello again, Panos!

Some positive news - I'm to the stage where my WordPress plugin now seems to recognize that your script exists. The bad news is that when I make a selection in the first select box, I briefly see the 'please wait' in the second select option box, but no option values are returned. My configuration is as follows:

1) I do not include top_script.php, as the database connections are taken care of by WordPress (I have tried including it and it does not affect behaviour in any way)
2) I have commented out the call to openDB() in sc_select_ajax_handler.php (does not seem to affect behaviour whether in or out)
3) I have an absolute (and correct) include address in my plugin file for sc_classes.php
4) in sc_select_ajax_handler.php, I have the same absolute include address for sc_classes.php - although the behaviour with or without that 'include' is the same
5) I have an absolute address for $php_ajax_handler in sc_classes - which I know to be correct. The odd thing is that when I intentionally introduce an error into that address, behaviour is not affected.
6) prototype.js is correctly referenced and available to the script

I'm not sure where to look next, and would welcome any further thoughts you may have.


Kind regards,

messageRe: Ajax Linked Select boxes and Wordpress
Posted by: panos (IP Logged)
Date: November 04, 2007 04:06PM

Welcome back Gramps,
sorry for the delay to respond but i had some troubles at work.

Let me introduce you to my usual debuging method for such issues.

First edit sc_classes.php and somewhere near line 580 you will see the line:

var myAjax = new Ajax.Request( url, { method: 'POST', parameters: pars, onComplete: onCompleteCallBack });

just before that add the following line:

$("dump_area").innerHTML = pars;

Next, in the page you placed select boxes, add somewhere a div

<div id="dump_area"></div>

Now refresh page and make a selection in the first box. In the div you will get parameter
string used to make ajax call to sc_select_ajax_handler.php.
Copy that and point your browser to sc_select_ajax_handler.php with that parameter string.
eg:

www.mydomain.com/plugins/sc_select_ajax_handler.php?linkval=2&table=lsd_demo_2&key=rec_id&text=descr&order=rec_id&extra_where=&select_prompt_text=Please Select&linkfld=parent_id&xml_encoding=ISO-8859-1

if it works you will get an xml document with select options for the second select box.
If you see strange messages "view source" to see the php error message.

This will give you information about the cause of the problem.

if that returns options for select box with out php error messages then db connection and parameters are correct.

Now next issue to check is if javascript points to the file correctly. Go back to sc_classes.php and change

$("dump_area").innerHTML = pars;

to

$("dump_area").innerHTML = url;

and refresh page and make a new selection (like before)
usually it is just sc_select_ajax_handler.php that means in root.

Those 2 things are usualy give me the solution.

And last note, openDB() sould not be commented in sc_select_ajax_handler.php because it is runned stand alone, no wordpress db connection present at that state.

If still you cant locate problem... i am here smiling smiley

best regards
panos

messageRe: Ajax Linked Select boxes and Wordpress
Posted by: gramps (IP Logged)
Date: November 05, 2007 12:33AM

Thank you Panos for your debugging tutorial - I'm sure I'll put it to good use many times!

I identified the first problem (database connection without the call to openDcool smiley, and am now getting an xml response. But the problem persists and I think it's in the url.

An article at [codex.wordpress.org] suggests the url should be set to domainname/wp-admin/admin-ajax.php Then a parameter 'action' should be added, with a value idenifying where to look in my plugin. Based on this, I have

1) hardcoded the url in sc_classes.php
2) added a parameter 'action=admatch_handler' within sc_classes.php (beween linkval and table)
3) added this to my plugin: add_action('wp_ajax_admatch_handler', 'admatch_ajax_handler' );
4) added a function to my plugin called 'admatch_ajax_handler', which consists of the content sc_select_ajax_handler.php plus includes for my database settings and sc_classes.php

I'd like to say it worked ... but it didn't. So it's back to the drawing board for me.


Kind regards,

messageRe: Ajax Linked Select boxes and Wordpress
Posted by: panos (IP Logged)
Date: November 07, 2007 10:03PM

Still studing wordpress documentation....

is there a function or something to get wordpress's url?

eg if wordpress in www.mydomain.com/blog/
getting [www.mydomain.com] from a function or a variable
it would solve the problem.
Cause then we add wp-includes/plugins/pluginname/ and we are done!

still looking, if anyone knows it please post it here

messageRe: Ajax Linked Select boxes and Wordpress
Posted by: panos (IP Logged)
Date: November 07, 2007 10:12PM

now i have something smiling smiley

in sc_classes.php and about line 196 make it like

var $php_ajax_handler =
get_settings('siteurl') . '/wp-content/plugins/myplugin/sc_select_ajax_handler.php' ;

one problem should be solved this way. For sure ajax hits ajax handler even in other directory.

does it gets any better with this change?

messageRe: Ajax Linked Select boxes and Wordpress
Posted by: panos (IP Logged)
Date: November 17, 2007 11:26AM

Well?

didnt helped?

changed to other lib? sad smiley

messageRe: Ajax Linked Select boxes and Wordpress
Posted by: gramps (IP Logged)
Date: November 18, 2007 02:39AM

Hi Panos


I'm very sorry I haven't gotten back to you! I've been up to my ears in a project I'm trying to put to bed and haven't gotten back to your nice script. I'll do it within a day or two!


Kind regards,



Sorry, only registered users may post in this forum.

All Rights Reserved 2006 Salix.gr  |  Design by Ades Design |  Hosting by e-emporio |  forum.Salix.gr powered by Phorum