| | |
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:
Solved Threads: 0
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-
phpScript.php-
php Syntax (Toggle Plain Text)
<?php print $divID; ?>
javascript Syntax (Toggle Plain Text)
function onDropEvent(draggable,droparea){ var url="phpScript.php"; var rand = Math.random(9999); var pars = 'divID=' + draggable.id + '&rand=' + rand; var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: showID} ); } function showID(myAjax){ var newVar=String(myAjax.responseText); alert(newVar); // <----alert shows expected divID if (newVar == 'close') { // <----not evaluated alert(newVar); } else { toggle(newVar); // <----newVar=divID (should) } } function toggle (elid) { //var elid="divID"; //<-- works this way var cdiv = document.getElementById(elid); //<-- doesn't work var pdiv = cdiv.parentNode; alert(cdiv.id +" "+ pdiv.id); //<-- empty without hardcode }
Hi UncleJoe,
I am not quite familiar on using this frameworks.
But i suspect, the problem could be on line #5, in your code:
if you can fix those lines above, im sure you will be able to sort things out.
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.
•
•
Join Date: Jul 2009
Posts: 16
Reputation:
Solved Threads: 0
•
•
•
•
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 theshowID(args)function? } );
if you can fix those lines above, im sure you will be able to sort things out.
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
UJ,
cdiv will be
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
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
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!
Preserving what we already have, let's try this and see if things will work:
hope it helps and hi to Airshow
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.
•
•
Join Date: Jul 2009
Posts: 16
Reputation:
Solved Threads: 0
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.
'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.
•
•
Join Date: Jul 2009
Posts: 16
Reputation:
Solved Threads: 0
•
•
•
•
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
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:
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:
- Fix the php such that it only returns the necessary string. It's good to put an
exitcommand immediately after printing the required output to ensure nothing gets tacked on further down the page. - 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.
Last edited by Airshow; Jul 13th, 2009 at 7:30 pm.
50% of the solution lies in accurately describing the problem!
Try this with your
toggle(arg) function: JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function toggle( elid ) { elid = elid.replace(/[\s\n]+/g, ""); var cdiv = (( document.getElementById ) ? document.getElementsByTagName("*") : (( document.all && !document.getElementById ) ? document.documentElement.all : null )); var elem = Boolean((((( cdiv ) ? cdiv[ elid ] : undefined )) ? true : false )); var pdiv = (( elem ) ? cdiv[ elid ].parentNode : undefined ); var testMSG = "\n" + (( pdiv ) ? "Element : " + cdiv[ elid ].id + "\nParent Element : " + pdiv.id : "(pdiv) is " + pdiv + "- please verify your element id!"); alert( testMSG ); }
Last edited by essential; Jul 13th, 2009 at 11:19 pm.
•
•
Join Date: Jul 2009
Posts: 16
Reputation:
Solved Threads: 0
•
•
•
•
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.
php Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- access JavaScript function return value from PHP (JavaScript / DHTML / AJAX)
- Why for loop in javascript is reseting a PHP variable (PHP)
- Is it possible to pass back both a return variable & reference variable in one funct? (C++)
- Move Javascript Variable to PHP (JavaScript / DHTML / AJAX)
- Pass a javascript variable to PHP in the same function (JavaScript / DHTML / AJAX)
- Passing a Javascript variable to a PHP using <script> call (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: popup with background disabled
- Next Thread: Having trouble with javascript array
Views: 1138 | Replies: 15
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
acid2 ajax ajaxcode ajaxhelp animate api array automatically beta boarder box bug button calendar captcha card cart checkbox codes column cookies createrange() css cursor decimal design dom download dropdown element enter error events firefox firehose flash focus form frameworks google gwt html htmlform iframe image() images index internet java javascript javascripts jawascriptruntimeerror jquery jsfile jsp listbox marquee masterpage menu microsoft mimic mp3 mp4 mysql offline onmouseover parameters php player post problem progressbar prototype rating redirect regex runtime scale search select session shopping size sources sql starrating text textarea toggle twitter validation w3c web website window windowofwords windowsxp wysiwyg xml xspf





