javascript not interpreting php return variable

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Jul 2009
Posts: 16
Reputation: UncleJoe is an unknown quantity at this point 
Solved Threads: 0
UncleJoe UncleJoe is offline Offline
Newbie Poster

javascript not interpreting php return variable

 
0
  #1
Jul 11th, 2009
I'm all hosed up...most of this is pretty new to me, so please forgive my terminology. I'm using scriptaculous for the DnD stuff. Using AJAX I'm returning a variable from the server. The variable can be verified using alert(variable), but when I pass the variable as an argument it isn't working...

phpScript.php-
  1.  
  2. <?php print $divID; ?>

  1. function onDropEvent(draggable,droparea){
  2. var url="phpScript.php";
  3. var rand = Math.random(9999);
  4. var pars = 'divID=' + draggable.id + '&rand=' + rand;
  5. var myAjax = new Ajax.Request( url, {method: 'get',
  6. parameters: pars, onComplete: showID} );
  7. }
  8.  
  9. function showID(myAjax){
  10. var newVar=String(myAjax.responseText);
  11. alert(newVar); // <----alert shows expected divID
  12. if (newVar == 'close') { // <----not evaluated
  13. alert(newVar);
  14. }
  15. else {
  16. toggle(newVar); // <----newVar=divID (should)
  17. }
  18. }
  19.  
  20. function toggle (elid) {
  21. //var elid="divID"; //<-- works this way
  22. var cdiv = document.getElementById(elid); //<-- doesn't work
  23. var pdiv = cdiv.parentNode;
  24. alert(cdiv.id +" "+ pdiv.id); //<-- empty without hardcode
  25. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: javascript not interpreting php return variable

 
0
  #2
Jul 12th, 2009
Hi UncleJoe,

I am not quite familiar on using this frameworks.

But i suspect, the problem could be on line #5, in your code:

var myAjax = new Ajax.Request( url, {
method : 'get',
// Try to provide object reference onto this label(pars).
pars : pars, // (OBJECT REFERENCE)?, 

onComplete : showID // How do you pass arguments inside the   showID(args)  function? 
} );

if you can fix those lines above, im sure you will be able to sort things out.
Last edited by essential; Jul 12th, 2009 at 2:36 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 16
Reputation: UncleJoe is an unknown quantity at this point 
Solved Threads: 0
UncleJoe UncleJoe is offline Offline
Newbie Poster

Re: javascript not interpreting php return variable

 
0
  #3
Jul 12th, 2009
Originally Posted by essential View Post
Hi UncleJoe,

I am not quite familiar on using this frameworks.

But i suspect, the problem could be on line #5, in your code:

var myAjax = new Ajax.Request( url, {
method : 'get',
// Try to provide object reference onto this label(pars).
pars : pars, // (OBJECT REFERENCE)?, 

onComplete : showID // How do you pass arguments inside the   showID(args)  function? 
} );

if you can fix those lines above, im sure you will be able to sort things out.
Thanks for your reply...essential! As I said I'm pretty new to this, if I don't understand your reply feel free to throw something at me. The 'pars' parameter is getting to php successfully, and the phpScript returns a value successfully, evidenced by the alert() function. The issue I am running into is assigning the returned value to a variable.

When I alert(myAjax.responseText), or alert(newVar), the popup box shows the divID I expect. Once newVar is passed to 'toggle' into 'elid' var cdiv is empty, then as expected pdiv is also empty. My thinking is 'newVar' should hold the returned string.

Aside from all that...is there a better way to accomplish what I want? Basically, I'm sending the dropped divID, 'draggable.id', to 'phpScript.php' for processing, then I want to return a new divID of which I can get the parent node. Ultimately, I'm setting the parent node to display:yes and updating the class of the returned divID. In order-

1) Send dropped divID to phpScript.php
2) Return new divID from phpScript
3) Set display:yes for divID.parentNode
4) Change css class for divID

I appreciate any help, this is the final hurdle...until the next one anyway!

UncleJoe
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 885
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 127
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: javascript not interpreting php return variable

 
0
  #4
Jul 12th, 2009
UJ,

cdiv will be null if no DOM element has the id of the variable elid .

Either the value returned by phpScript.php is wrong (it may just need trimming) or it is correct but the div in question is wrongly id'd or doesn't exist at all.

When there is a perfect match between elid and the id of an existing DOM element (hopefully the div you're looking for) then alert(cdiv); should give "[object]"

The code you posted looks fine.

I think the problem must lie elsewhere; either in the php/HTML that (is supposed to have) created the div in question, or the code in phpSctipt.php that (is supposed to) return a valid div id.

By the way ... hi to Ess.

Airshow
Last edited by Airshow; Jul 12th, 2009 at 8:42 pm.
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: javascript not interpreting php return variable

 
0
  #5
Jul 12th, 2009
Preserving what we already have, let's try this and see if things will work:

var new Ajax.Request( url, {
method : 'get',
// Try to provide object reference onto this label(pars).
pars, 
onComplete : function() { showID(myAjax); } 
} );

hope it helps and hi to Airshow
Last edited by essential; Jul 12th, 2009 at 10:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 16
Reputation: UncleJoe is an unknown quantity at this point 
Solved Threads: 0
UncleJoe UncleJoe is offline Offline
Newbie Poster

Re: javascript not interpreting php return variable

 
0
  #6
Jul 12th, 2009
Thanks for the reply Airshow. I double checked the scripts. The return from php is an existing div ID, verified by: alert(newVar);
'elid' is the same div ID verified by alert(elid); The div ID I'm testing is hardcoded in php to return, so PHP drops out of the equation. Finally, the div ID exists, verified by 'view source'. Still no value for 'cdiv'. A final test, I set elid=divID; and I get a return value for 'cdiv'.

What's puzzling me is the string divID is definitely there, and its definitely in 'elid', yet 'cdiv' returns empty (not null) unless its hard coded.

Is there some type of "treatment" a return string needs to go through from PHP to Javascript? Single quotes, double quotes, or something simple I may be missing?

I'm about to scrap all of it and start over. This was put together off a tutorial, maybe I should try a different one, of course, I'll be awful torqued when I run into the same problem again!

Thanks a million for your help, my wife is about to send me to Swearers Anonymous.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 16
Reputation: UncleJoe is an unknown quantity at this point 
Solved Threads: 0
UncleJoe UncleJoe is offline Offline
Newbie Poster

Re: javascript not interpreting php return variable

 
0
  #7
Jul 12th, 2009
Originally Posted by essential View Post
Preserving what we already have, let's try this and see if things will work:

var new Ajax.Request( url, {
method : 'get',
// Try to provide object reference onto this label(pars).
pars, 
onComplete : function() { showID(myAjax); } 
} );

hope it helps and hi to Airshow

Essential-
I tried to replace my ajax call with your line. The DnD quit working, I tinkered with it a little, but didn't have any luck. I'm going to try to look up the syntax you used, I'm not clear how the function() call works.

Thanks for the help, I know its tough to diagnose a problem without it sitting in front of you.

UncleJoe
Last edited by UncleJoe; Jul 12th, 2009 at 11:52 pm. Reason: fat fingers
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 885
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 127
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: javascript not interpreting php return variable

 
0
  #8
Jul 13th, 2009
UJ,

Burried in my post above is a suggestion that the returned divID might need to be stripped. ie. it may have leading/trailing white space that doesn't show up in a javascript alert.

You can test the theory with alert(divID.length). See if it reports the expected number of characters.

If this turns out to be the problem then you can do one (or both) of two things:
  1. Fix the php such that it only returns the necessary string. It's good to put an exit command immediately after printing the required output to ensure nothing gets tacked on further down the page.
  2. Trim the returned string in jvascript. This is a bit tricky if you don't know what to trim off, so method 1 is preferred.
Airshow
Last edited by Airshow; Jul 13th, 2009 at 7:30 pm.
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: javascript not interpreting php return variable

 
0
  #9
Jul 13th, 2009
Try this with your toggle(arg) function:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function toggle( elid ) {
  2. elid = elid.replace(/[\s\n]+/g, "");
  3. var cdiv = (( document.getElementById ) ? document.getElementsByTagName("*") : (( document.all && !document.getElementById ) ? document.documentElement.all : null ));
  4.  
  5. var elem = Boolean((((( cdiv ) ? cdiv[ elid ] : undefined )) ? true : false ));
  6.  
  7. var pdiv = (( elem ) ? cdiv[ elid ].parentNode : undefined );
  8.  
  9. var testMSG = "\n" + (( pdiv ) ? "Element : " + cdiv[ elid ].id + "\nParent Element : " + pdiv.id : "(pdiv) is " + pdiv + "- please verify your element id!");
  10. alert( testMSG );
  11. }
Last edited by essential; Jul 13th, 2009 at 11:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 16
Reputation: UncleJoe is an unknown quantity at this point 
Solved Threads: 0
UncleJoe UncleJoe is offline Offline
Newbie Poster

Re: javascript not interpreting php return variable

 
0
  #10
Jul 14th, 2009
Originally Posted by Airshow View Post
UJ,

Burried in my post above is a suggestion that the returned divID might need to be stripped. ie. it may have leading/trailing white space that doesn't show up in a javascript alert.

You can test the theory with alert(divID.length). See if it reports the expected number of characters.
BINGO!!!! I checked the string length which should be '3', but the alert returns '5'. I do have an exit in the php immediately after

  1. print $divID;

to no avail. I'm not familiar with stripping strings in javascript. I guess the first thing I need to know is where in the 5 element string the actual element is, ie two leading blanks, two trailing blanks, or one on either side.

Some of the returned div IDs are 4 characters, so I'll check if 2 extra characters is consistent. Could you point me in the right direction wrt string manipulation? I tried to trim the return in php, but that didn't help.

Thanks a bunch for your help and patience.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1138 | Replies: 15
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC