possible to "catch" a javascript-variable to put it into php

A place for users and developers of the Xataface to discuss and receive support.

Postby shannah » Wed May 30, 2007 11:20 am

Hi Markus,

None of the variables here are following any naming conventions.Ê You are free to change them as you like.

A couple of tips.Ê XXX has no properties, generally means that the variable is null or not initialized.Ê Generally this means that we have made a mistake somewhere when we thought we had initialized it.

The list_event_group variable should have been initialized in the updateTypesList function when we set it to list_event_groups_select (which should be a reference to the first select list).

What i usually do in these cases is start inserting alert() functions in various places to see what values my variables have (this is like a poor-man's debugger).

e.g.Ê In the updateTypesList() function, try calling alert(list_event_groups_select), and see what it says (it will pop up a javascript alert).ÊÊ Try this in various places to see where we are losing it.


-Steve
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby Markus » Wed May 30, 2007 11:33 am

Thanks again for your tips, Steve. The alert-thing sounds pretty similar to what I know from echo in php. I will try and give you response when I am through with it. Seems that I will have to learn some JavaScript right now ;)

Markus
Markus
 
Posts: 94
Joined: Wed Dec 31, 1969 5:00 pm

Postby shannah » Wed May 30, 2007 11:50 am

Thanks for being a pioneer here.Ê I have thought about trying to add this sort of functionality simply using the fields.ini file, but I want to make sure that I do it right in a generic sort of way.. may be a few releases from now.



-Steve

--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby Markus » Fri Jun 01, 2007 5:29 am

Hi steve,

My updateTypesList function again:

require(DATAFACE_SITE_URL+'/js/ajax.js');
var types_http = null;
var type_group = null;

/**
* updateTypesList
* This function is to be assigned in the onchange handler for the group types select list.
*/

function updateTypesList(type_groups_select){

// type_groups_select is a reference to the type_groups select list
var selectedGroup = type_groups_select.options[type_group.selectedIndex].value;
var url = DATAFACE_SITE_HREF +'?-action=get_types&type_group='.escape(selectedGroup);
types_http.open("GET", url);
types_http.onreadystatechange = handleUpdateTypesList;
// Assign the handleUpdateTypesList function as a handler to be called when the HTTP object receives a response from the server
type_group = type_groups_select;
// Save a reference to the group types select list in the http object so that it can be accessed from the handleUpdateTypesList function
types_http.send(null);
}

I found out that already in the first line:
var selectedGroup = type_groups_select.options[type_group.selectedIndex].value;

my firefox error-console tells me that type_group has no properties. I can do an alert("" +type_group+ "") above this line and get null which is defined above and so it works.

If I put the same alert after this line nothing will be returned when changing the select. Even if I put alert("Hello") there, nothing happens. It seems to me that this line has a mistake in it but could not figure out where it might be. I suppose, this line should return the Index of my value, right? Cause there are 5 values, it should return either 1,2,3,4 or 5. But it does not.

It also seems that the script stops here because I never get a changed URL when selecting something from my type_groups select. The URL stays like http://pathtomyapp/index.php?-action=new&-table=event
It is somehow frustrating.

Again also the relevant part of my fields.ini of table event:

[L_GID]
widget:label = "Ereignisgruppen ID"
widget:description = "Die Ereignisgruppen ID des Ereignisses"
widget:type = "select"
vocabulary = "list_event_group"
widget:atts:onchange="updateTypesList(this);"

[L_TID]
widget:label = "Ereignistyp ID"
widget:description = "Die Ereignistyp ID des Ereignisses"
widget:type = "select"
vocabulary = "list_event_type"

My valuelists.ini

[list_event_group]
__sql__ = "select l_gid, type_group from list_event_group"

[list_event_type]
__sql__ = "select l_tid, L_GID, type from list_event_type"

My relationships.ini

[list_event_group]
list_event_group.l_gid = "$L_GID"

[list_event_type]
list_event_type.l_tid = "$L_TID"

My get_types.php



Is this all correct so far? Do you have another idea, what could be wrong?

Thank you

Markus
Markus
 
Posts: 94
Joined: Wed Dec 31, 1969 5:00 pm

Postby shannah » Fri Jun 01, 2007 3:22 pm

Here is one mistake:

var selectedGroup = type_groups_select.options[type_group.selectedIndex].value;

This should be

var selectedGroup = type_groups_select.options[type_groups_select.selectedIndex].value;

--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby Markus » Sun Jun 03, 2007 8:12 am

Here is one mistake:

var selectedGroup = type_groups_select.options[type_group.selectedIndex].value;

This should be

var selectedGroup = type_groups_select.options[type_groups_select.selectedIndex].value;




Hi steve,

thank you again. Let me resume what I have done so far by posting the code of updateTypesList again. I put some comments into the code to show, what is happening.


require(DATAFACE_SITE_URL+'/js/ajax.js');
var types_http = null;
var type_group = null;

/**
* updateTypesList
* This function is to be assigned in the onchange handler for the group types select list.
*/

function updateTypesList(type_groups_select){
// type_groups_select is a reference to the type_groups select list

var selectedGroup = type_groups_select.options[type_groups_select.selectedIndex].value;

alert ("" +selectedGroup+ ""); // works fine, shows me the right ID of the selected value; for example 2

var url = DATAFACE_SITE_HREF +'?-action=get_types&type_group='+selectedGroup;

alert ("" +url+ ""); // shows me the url which is: /dataface/plone_javascript_variables.js.php?-action=get_types&type_group=2 ;

alert ("" +types_http+ ""); // shows me null

types_http.open("GET" url); // does not work until now

types_http.onreadystatechange = handleUpdateTypesList;
// Assign the handleUpdateTypesList function as a handler to be called when the HTTP object receives a response from the server
type_group = type_groups_select;
// Save a reference to the group types select list in the http object so that it can be accessed from the handleUpdateTypesList function
types_http.send(null);
}

After I have closed all the alerts I get an error in firefox error-console: types_http has no properties

I wonder why the DATAFACE_SITE_HREF gives back /dataface/plone_javascript_variables.js.php and if this could be right?

There is no /dataface directory within my app because I have all the dataface files one step up the hierarchy on the server like:

server_root/dataface
server_root/pathtomyapp

Should I better have my application directory within the dataface directory?

So I would like to ask if this URL is right and why the types_http has no properties. I think I am not so far from a solution of my problem now but I am not enough into DF to do it on my own.

Possibly you are not working on sundays which I don't do normally too. But I really want to find the solution and understand what is happening in my DF-application. Maybe you can help me a little more. Could be tomorrow of course ;)

greets

Markus
Markus
 
Posts: 94
Joined: Wed Dec 31, 1969 5:00 pm

Postby shannah » Sun Jun 03, 2007 5:20 pm

Hi Markus,

This readout makes sense.Ê First some responses to specific questions:
Q: "I wonder why the DATAFACE_SITE_HREF gives back /dataface/plone_javascript_variables.js.php and if this could be right?"
A: Ahh.. Yes.. This looks like a little bug in Dataface.Ê I have updated my current source base to fix this bug.Ê You can fix it in your version by opening the file: Dataface/templates/Dataface_Main_Template.html.Ê At around Line 51, there some HTML like:

Add a couple of lines so that it becomes:

This should produce the correct url.
Q: "Should I better have my application directory within the dataface directory?"
A: No.. My preference is to do it the way you are currently doing it:Ê with separate directories for dataface and your application.

The reason for the error: "types_http has no properties" is because you have removed the code that was there before to set the types_http variable to an HTTP object:
if ( types_http == null ) types_http = getHTTPObject();

I also notice a typo from the code that I made to include the ajax.js file.Ê It is currently :
require(DATAFACE_SITE_URL+'/js/ajax.js');

It should be
require(DATAFACE_URL+'/js/ajax.js');


Best regards
Steve
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby Markus » Mon Jun 04, 2007 4:08 am

Dear steve,

thank you again for your explanations and changes. I made all the changes to Dataface_Main_Template.html and in my javascript.js I have now:

require(DATAFACE_URL+'/js/ajax.js');

var types_http = null;
var type_group = null;

/**
* updateTypesList
* This function is to be assigned in the onchange handler for the group types select list.
*/

function updateTypesList(type_groups_select){
// type_groups_select is a reference to the type_groups select list

if ( types_http == null ) types_http = getHTTPObject();

var selectedGroup = type_groups_select.options[type_groups_select.selectedIndex].value;

alert ("" +selectedGroup+ ""); //shows the Index of selectedGroup, for example 2

var url = DATAFACE_SITE_HREF +'?-action=get_types&type_group='+selectedGroup;

alert ("" +url+ ""); // shows the url, which is: /dataface/plone_javascript_variables.js.php?-action=get_types&type_group=2 ;

alert ("" +types_http+ ""); // firefox shows [object XMLHttpRequest] ie shows [object]

types_http.open("GET", url);


types_http.onreadystatechange = handleUpdateTypesList;
// Assign the handleUpdateTypesList function as a handler to be called when the HTTP object receives a response from the server

type_group = type_groups_select;
// Save a reference to the group types select list in the http object so that it can be accessed from the handleUpdateTypesList function

types_http.send(null);
}

/**
* handleUpdateTypesList
* This function is called when a response is received from the HTTP object after we request the types for a particular group.
*/
function handleUpdateTypesList(){
if ( types_http.readystate == 4 ){
// We have successfully obtained the response from the server
if ( types_http == null ) types_http = getHTTPObject();
var options = eval('(' + types_http.responseText +')');
// we now have the options that we will use to fill the 2nd select list as an array
// now let's get out select list and refill it
var types_list = type_group.form.elements['type_group'];
// We obtain a reference to the type_id select list (let's assume that the field is named type_id.. if not, then change this).
// Clear the existing options in the types list.
for ( var i=0; i types_list.options[i] = new Option(options[i]['type'], options[i]['l_tid']);
}
}

}


See my comments in updateTypesList function above. I don't get any errors from firefox now but still the url does not change and the second select remains empty (please select...)

My current get_types.php is like follows (I leave the php-questionmarks)

class actions_get_types {
function handle(&$params){
if ( isset($_REQUEST['type_group']) ){
$sql = "select l_tid, L_GID, type from list_event_type where L_GID='".addslashes($_REQUEST['type_group'])."'";
} else {
$sql = "select l_tid, L_GID, type from list_event_type";
}
$res = mysql_query($sql, df_db());
if ( !$res ) trigger_error(mysql_error(df_db()), E_USER_ERROR);
header('Content-type: text/javascript');
echo '[';
$values = array();
while ( $row = mysql_fetch_assoc($res) ){
$values[] = "{l_tid:'".$row['l_tid']."', type: '".addslashes($row['type'])."'}";
}
echo implode(',',$values);
echo ']';
exit;
}

}

Obviously there must be something wrong with the following line in updateTypesList:

types_http.open("GET", url);

because it is not executed, right? I am not sure about what I can put here as an alert to check what's happening. Maybe you know?
Please have a close look at my get_types.php if this is right. Especially I am not sure about these lines:

if ( isset($_REQUEST['type_group']) ){
$sql = "select l_tid, L_GID, type from list_event_type where L_GID='".addslashes($_REQUEST['type_group'])."'";

and

while ( $row = mysql_fetch_assoc($res) ){
$values[] = "{l_tid:'".$row['l_tid']."', type: '".addslashes($row['type'])."'}";

Can you give me some hints again?

Markus
Markus
 
Posts: 94
Joined: Wed Dec 31, 1969 5:00 pm

Postby shannah » Mon Jun 04, 2007 8:07 am

OK.. next phase of problem solving: getting AJAX to work.ÊÊ Check a couple of things:

1. Put some alerts in the handleUpdateTypesList function to at least see if it ever gets called.

2. If handleUpdateTypesList is getting called, then you want to take a look at the response text.Ê (e.g. alert(types_http.responseText).

3. If handleUpdateTypesList is NOT getting called, then you'll want to take a look at the url that is being sent to types_http.open().Ê Copy and paste this URL directly into your web browser and see what it gives you...Ê this url should result in the output of your custom action.

Best regards


Steve
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby Markus » Mon Jun 04, 2007 9:07 am

OK.. next phase of problem solving: getting AJAX to work. Check a couple of things:

1. Put some alerts in the handleUpdateTypesList function to at least see if it ever gets called.

2. If handleUpdateTypesList is getting called, then you want to take a look at the response text. (e.g. alert(types_http.responseText).

3. If handleUpdateTypesList is NOT getting called, then you'll want to take a look at the url that is being sent to types_http.open(). Copy and paste this URL directly into your web browser and see what it gives you... this url should result in the output of your custom action.

Best regards


Steve



Hi steve,

I tried the alerts in handleUpdateTypesList function and they won't run.

I gave the url /dataface/plone_javascript_variables.js.php?-action=get_types&type_group=2 directly into the browser and it calls the plone_javascript_variables.js.php

The output is

// Global Plone variables that need to be accessible to the Javascripts

//portal_url = 'http://localhost/~shannah/lesson_plans';
portal_url = '/dataface';
DATAFACE_URL = portal_url;
DATAFACE_SITE_URL = '/dataface';
DATAFACE_SITE_HREF = '/dataface/plone_javascript_variables.js.php';

How can I get to my custom action?

Markus
Markus
 
Posts: 94
Joined: Wed Dec 31, 1969 5:00 pm

Postby shannah » Mon Jun 04, 2007 9:21 am

Ok.. for some reason it's not picking up the new values for DATAFACE_SITE_HREF that you put into the dataface template.... for the sake of getting this working, just hard code the url for now.

i.e. swap DATAFACE_SITE_HREF with just 'index.php'

Your custom action should be accessed by index.php?-action=get_types&type_group=%selectedGroup%Ê (where %selectedGroup% is the id of the group whose options you want to get.

-Steve

--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby Markus » Mon Jun 04, 2007 10:25 am

Ok.. for some reason it's not picking up the new values for DATAFACE_SITE_HREF that you put into the dataface template.... for the sake of getting this working, just hard code the url for now.

i.e. swap DATAFACE_SITE_HREF with just 'index.php'

Your custom action should be accessed by index.php?-action=get_types&type_group=%selectedGroup% (where %selectedGroup% is the id of the group whose options you want to get.

-Steve

>



It is quite persistent. Now it gives me index is not defined :(

Any idea?

Markus
Markus
 
Posts: 94
Joined: Wed Dec 31, 1969 5:00 pm

Postby shannah » Mon Jun 04, 2007 10:57 am

Hi Markus,

Without a line number/source code it is difficult or impossibleÊ for me to speculate as to the source of this error.

--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby Markus » Mon Jun 04, 2007 1:50 pm

Hi Markus,

Without a line number/source code it is difficult or impossible for me to speculate as to the source of this error.



Hi Steve,

sorry, i just changed the line you said. I marked it OLD and NEW inside the comment of code. Now my javascript.js is:

require(DATAFACE_URL+'/js/ajax.js');

var types_http = null;
var type_group = null;

/**
* updateTypesList
* This function is to be assigned in the onchange handler for the group types select list.
*/

function updateTypesList(type_groups_select){
// type_groups_select is a reference to the type_groups select list

if ( types_http == null ) types_http = getHTTPObject();

var selectedGroup = type_groups_select.options[type_groups_select.selectedIndex].value;

alert ("" +selectedGroup+ ""); //shows the Index of selectedGroup, for example 2

//var url = DATAFACE_SITE_HREF +'?-action=get_types&type_group='+selectedGroup; // OLD
var url = index.php +'?-action=get_types&type_group='+selectedGroup; // NEW

alert ("" +url+ ""); // shows the url, which is: /dataface/plone_javascript_variables.js.php?-action=get_types&type_group=2 ;

alert ("" +types_http+ ""); // shows [objekt XMLHttpRequest]

types_http.open("GET", url);


types_http.onreadystatechange = handleUpdateTypesList;
// Assign the handleUpdateTypesList function as a handler to be called when the HTTP object receives a response from the server
type_group = type_groups_select;
// Save a reference to the group types select list in the http object so that it can be accessed from the handleUpdateTypesList function
types_http.send(null);
}

/**
* handleUpdateTypesList
* This function is called when a response is received from the HTTP object after we request the types for a particular group.
*/
function handleUpdateTypesList(){
if ( types_http.readystate == 4 ){
alert ("Hello");
// We have successfully obtained the response from the server
if ( types_http == null ) types_http = getHTTPObject();

alert ("" +types_http.responseText+ "");

var options = eval('(' + types_http.responseText +')');

// we now have the options that we will use to fill the 2nd select list as an array
// now let's get out select list and refill it
var types_list = type_group.form.elements['type_group'];
// We obtain a reference to the type_id select list (let's assume that the field is named type_id.. if not, then change this).
// Clear the existing options in the types list.
for ( var i=0; i types_list.options[i] = new Option(options[i]['type'], options[i]['l_tid']);
}
}

}


I get an error "index is not defined" in my firefox error-console. Do I have a syntax-error somewhere?

Thanks

Markus
Markus
 
Posts: 94
Joined: Wed Dec 31, 1969 5:00 pm

Postby shannah » Mon Jun 04, 2007 1:55 pm

Yes.Ê index.php is a string so it has to be inside quotes.Ê Right now javascript is treating it like a variable.

i.e. It thinks that you are referring to variable named 'index' which is an object that has a member variable 'php'... so it is complaining that the 'index' variable has no index 'php'.

If that makes sense.
Best regards
Steve
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

PreviousNext

Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 23 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved