943,776 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Jul 11th, 2009
0

javascript not interpreting php return variable

Expand Post »
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-
php Syntax (Toggle Plain Text)
  1.  
  2. <?php print $divID; ?>

javascript Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
UncleJoe is offline Offline
20 posts
since Jul 2009
Jul 12th, 2009
0

Re: javascript not interpreting php return variable

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.
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Jul 12th, 2009
0

Re: javascript not interpreting php return variable

Click to Expand / Collapse  Quote originally posted by essential ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
UncleJoe is offline Offline
20 posts
since Jul 2009
Jul 12th, 2009
0

Re: javascript not interpreting php return variable

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.
Sponsor
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,526 posts
since Apr 2009
Jul 12th, 2009
0

Re: javascript not interpreting php return variable

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.
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Jul 12th, 2009
0

Re: javascript not interpreting php return variable

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
UncleJoe is offline Offline
20 posts
since Jul 2009
Jul 12th, 2009
0

Re: javascript not interpreting php return variable

Click to Expand / Collapse  Quote originally posted by essential ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
UncleJoe is offline Offline
20 posts
since Jul 2009
Jul 13th, 2009
0

Re: javascript not interpreting php return variable

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.
Sponsor
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,526 posts
since Apr 2009
Jul 13th, 2009
0

Re: javascript not interpreting php return variable

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.
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Jul 14th, 2009
0

Re: javascript not interpreting php return variable

Click to Expand / Collapse  Quote originally posted by Airshow ...
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

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
UncleJoe is offline Offline
20 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Problem in tabbed pane working by java script
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Having trouble with javascript array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC