PDA

View Full Version : Blank Variables



techno_race
07-26-2008, 04:38 PM
I have a PHP script that returns a JavaScript. The JavaScript returns correctly, except the PHP-defined variables are...blank.
here is the PHP:

echo "<script type=\"text/javascript\">var sfonts = " . $sfonts . "; var ssounds = " . $ssounds . "; var swallpaper = " . $swallpaper . "; var sicons = " . $sicons . "; var ssoundtracks = " . $ssoundtracks . "; var sprograms = " . $sprograms . "; var dfonts = " . $dfonts . "; var dsounds = " . $dsounds . "; var dwallpaper = " . $dwallpaper . "; var dicons = " . $dicons . "; var dsoundtracks = " . $dsoundtracks . "; var dprograms = " . $dprograms . "; document.getElementById('textfield').value = sfonts; document.getElementById('textfield2').value = dfonts; document.getElementById('textfield3').value = ssounds; document.getElementById('textfield8').value = dsounds; document.getElementById('textfield4').value = swallpaper; document.getElementById('textfield9').value = dwallpaper; document.getElementById('textfield5').value = sicons; document.getElementById('textfield10').value = dicons; document.getElementById('textfield6').value = ssoundtracks; document.getElementById('textfield11').value = dsoundtracks; document.getElementById('textfield7').value = sprograms; document.getElementById('textfield12').value = dprograms; var sscore = 0; var dscore = 0; if (sfonts > dfonts) { document.getElementById('textfield2').value = ''; sscore = sscore + 1; } if (dfonts > sfonts) { document.getElementById('textfield').value = ''; dscore = dscore + 1; } if (dfonts == sfonts) { sscore = sscore + 1; dscore = dscore + 1; } if (ssounds > dsounds) { document.getElementById('textfield8').value = ''; sscore = sscore + 1; } if (dsounds > ssounds) { document.getElementById('textfield3').value = ''; dscore = dscore + 1; } if (dsounds == ssounds) { sscore = sscore + 1; dscore = dscore + 1; } if (swallpaper > dwallpaper) { document.getElementById('textfield9').value = ''; sscore = sscore + 1; } if (dwallpaper > swallpaper) { document.getElementById('textfield4').value = ''; dscore = dscore + 1; } if (dwallpaper == swallpaper) { sscore = sscore + 1; dscore = dscore + 1; } if (sicons > dicons) { document.getElementById('textfield10').value = ''; sscore = sscore + 1; } if (dicons > sicons) { document.getElementById('textfield5').value = ''; dscore = dscore + 1; } if (dicons == sicons) { sscore = sscore + 1; dscore = dscore + 1; } if (ssoundtracks > dsoundtracks) { document.getElementById('textfield11').value = ''; sscore = sscore + 1; } if (dsoundtracks > ssoundtracks) { document.getElementById('textfield6').value = ''; dscore = dscore + 1; } if (dsoundtracks == ssoundtracks) { sscore = sscore + 1; dscore = dscore + 1; } if (sprograms > dprograms) { document.getElementById('textfield12').value = ''; sscore = sscore + 1; } if (dprograms > sprograms) { document.getElementById('textfield7').value = ''; dscore = dscore + 1; } if (dprograms == sprograms) { sscore = sscore + 1; dscore = dscore + 1; } document.write('UCD ' + sscore + ', ST ' + dscore); if (sscore > dscore) { alert('The Ultimate Customization Discs win!'); } if (dscore > sscore) { alert('SuperTweak wins!'); } if (dscore == sscore) { alert('It\'s a tie!'); }</script>";
If i go to tally.php?sfonts=85&ssounds=825&swallpaper=45232&sicons=474&ssoundtracks=07&sprograms=777&dfonts=52&dsounds=54&dwallpaper=04&dicons=25&dsoundtracks=0&dprograms=0, it returns:

<script type="text/javascript">var sfonts = ; var ssounds = ; var swallpaper = ; var sicons = ; var ssoundtracks = ; var sprograms = ; var dfonts = ; var dsounds = ; var dwallpaper = ; var dicons = ; var dsoundtracks = ; var dprograms = ; document.getElementById('textfield').value = sfonts; document.getElementById('textfield2').value = dfonts; document.getElementById('textfield3').value = ssounds; document.getElementById('textfield8').value = dsounds; document.getElementById('textfield4').value = swallpaper; document.getElementById('textfield9').value = dwallpaper; document.getElementById('textfield5').value = sicons; document.getElementById('textfield10').value = dicons; document.getElementById('textfield6').value = ssoundtracks; document.getElementById('textfield11').value = dsoundtracks; document.getElementById('textfield7').value = sprograms; document.getElementById('textfield12').value = dprograms; var sscore = 0; var dscore = 0; if (sfonts > dfonts) { document.getElementById('textfield2').value = ''; sscore = sscore + 1; } if (dfonts > sfonts) { document.getElementById('textfield').value = ''; dscore = dscore + 1; } if (dfonts == sfonts) { sscore = sscore + 1; dscore = dscore + 1; } if (ssounds > dsounds) { document.getElementById('textfield8').value = ''; sscore = sscore + 1; } if (dsounds > ssounds) { document.getElementById('textfield3').value = ''; dscore = dscore + 1; } if (dsounds == ssounds) { sscore = sscore + 1; dscore = dscore + 1; } if (swallpaper > dwallpaper) { document.getElementById('textfield9').value = ''; sscore = sscore + 1; } if (dwallpaper > swallpaper) { document.getElementById('textfield4').value = ''; dscore = dscore + 1; } if (dwallpaper == swallpaper) { sscore = sscore + 1; dscore = dscore + 1; } if (sicons > dicons) { document.getElementById('textfield10').value = ''; sscore = sscore + 1; } if (dicons > sicons) { document.getElementById('textfield5').value = ''; dscore = dscore + 1; } if (dicons == sicons) { sscore = sscore + 1; dscore = dscore + 1; } if (ssoundtracks > dsoundtracks) { document.getElementById('textfield11').value = ''; sscore = sscore + 1; } if (dsoundtracks > ssoundtracks) { document.getElementById('textfield6').value = ''; dscore = dscore + 1; } if (dsoundtracks == ssoundtracks) { sscore = sscore + 1; dscore = dscore + 1; } if (sprograms > dprograms) { document.getElementById('textfield12').value = ''; sscore = sscore + 1; } if (dprograms > sprograms) { document.getElementById('textfield7').value = ''; dscore = dscore + 1; } if (dprograms == sprograms) { sscore = sscore + 1; dscore = dscore + 1; } document.write('UCD ' + sscore + ', ST ' + dscore); if (sscore > dscore) { alert('The Ultimate Customization Discs win!'); } if (dscore > sscore) { alert('SuperTweak wins!'); } if (dscore == sscore) { alert('It\'s a tie!'); }</script>
Does anyone have any idea what might be causing this?

Dal
07-26-2008, 04:46 PM
"except the PHP-defined" - this straightaway leads me to believe that there is a problem with the scope of these variables but I cant see the rest of your code. Make sure that this is being addressed.

Kind regards
Dal

techietim
07-26-2008, 05:13 PM
Hmm, you're probably thinking that variables defined with GET are automatically turned into global variables. That is not the case, unless register global's in on, which is should not be.

Just change those variables to get the data from $_GET
Example:


$_GET['sfonts']

techno_race
07-26-2008, 05:26 PM
That's still not working.

Dal
07-26-2008, 05:36 PM
Well we could all try guessing some more or you could post some more code because to debug we need more than whats provided;

where does $sfonts, $ssounds, $swallpaper etc come from? Code please ;)

Thanks
Dal

Jas
07-27-2008, 04:06 AM
You might try echoing out the variables at various locations to see what's going on with them. If that doesn't help, you will need to post the rest of the function/code.

techno_race
07-27-2008, 04:41 PM
That is the rest of my code.

tally.php?sfonts=85&ssounds=825&swallpaper=45232&sicons=474&ssoundtracks=07&sprograms=777&dfonts=52&dsounds=54&dwallpaper=04&dicons=25&dsoundtracks=0&dprograms=0
What other code is there?
EDIT: Does anyone know why it puts a ' ' between '=' and '0' at the end? There's no ' ' in the BBCode.

Dal
07-27-2008, 07:06 PM
That is the rest of my code.

What other code is there?
EDIT: Does anyone know why it puts a ' ' between '=' and '0' at the end? There's no ' ' in the BBCode.

If I created a script that pulled in a query string and used it to create javascript then I would expect more code than that. If thats all you have then thats why it doesnt work. I would recommend looking through a tutorial to find out how to use a query string.

?somevar=test
echo $_GET['somevar'];

will output;
test

but
echo somevar;

will output;
(Null)

motormichael12
07-28-2008, 01:55 AM
EDIT: Does anyone know why it puts a ' ' between '=' and '0' at the end? There's no ' ' in the BBCode.

That line is 150 characters, probably the default so that the forum layout isn't too distorted.