...

View Full Version : PHP and Javascript



IFeelYourPain
03-02-2008, 06:41 AM
I can't make them work together. The quotations in PHP = out with the quotations in the javascript. What can I do?

_Aerospace_Eng_
03-02-2008, 06:49 AM
You need to escape the quotes.

echo '<a href="test.html" onclick="dosomething(\'blah\');return false">test</a>';

IFeelYourPain
03-02-2008, 07:31 AM
I tried, but its giving me a blank page. I know it has something to do with putting ' somewhere, but don't know where:


<?php
if ($vbulletin->userinfo['usergroupid'] == '34'
or $vbulletin->userinfo['usergroupid'] == '6'
)
{
echo "
<div style="margin:20px; margin-top:5px">
<div class="smallfont" style="margin-bottom:2px"><b>Spoiler</b> for <i>{option}</i>: <input type="button" value="Show" style="width:45px;font-size:10px;margin:0px;padding:0px;" onClick="if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = ''; this.innerText = ''; this.value = 'Hide'; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = 'Show'; }">
</div>
<div class="alt2" style="margin: 0px; padding: 6px; border: 1px inset;">
<div style="display: none;">
{param}
</div>
</div>
</div>

";
} else {
echo "You do not have permission for this page"; }
?>


How should I write that so it works?

_Aerospace_Eng_
03-02-2008, 07:37 AM
Its not the javascript that is the problem. You are trying to echo out html that has double quotes but you don't escape them. Try this

<?php
if ($vbulletin->userinfo['usergroupid'] == '34'
or $vbulletin->userinfo['usergroupid'] == '6'
)
{
echo <<<EOF
<div style="margin:20px; margin-top:5px">
<div class="smallfont" style="margin-bottom:2px"><b>Spoiler</b> for <i>{option}</i>: <input type="button" value="Show" style="width:45px;font-size:10px;margin:0px;padding:0px;" onClick="if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = ''; this.innerText = ''; this.value = 'Hide'; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = 'Show'; }">
</div>
<div class="alt2" style="margin: 0px; padding: 6px; border: 1px inset;">
<div style="display: none;">
{param}
</div>
</div>
</div>

EOF;
} else {
echo "You do not have permission for this page"; }
?>

IFeelYourPain
03-02-2008, 07:38 AM
Double quotes where? Whats the EOF thing?

_Aerospace_Eng_
03-02-2008, 07:42 AM
You have double quotes in your HTML yet you use echo " without escaping your double quotes. You can't do that.

As to the EOF thing I suggest you start getting more familiar with php. When in doubt always read the manual.

Thats called heredoc syntax (http://us2.php.net/types.string). On the link there is a section titled heredoc.

Your original code will work if you use a single quote on the echo but now you need to escape the single quotes on your javascript call.

<?php
if ($vbulletin->userinfo['usergroupid'] == '34'
or $vbulletin->userinfo['usergroupid'] == '6'
)
{
echo '
<div style="margin:20px; margin-top:5px">
<div class="smallfont" style="margin-bottom:2px"><b>Spoiler</b> for <i>{option}</i>: <input type="button" value="Show" style="width:45px;font-size:10px;margin:0px;padding:0px;" onClick="if (this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display != \'\') { this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display = \'\'; this.innerText = \'\'; this.value = \'Hide\'; } else { this.parentNode.parentNode.getElementsByTagName(\'div\')[1].getElementsByTagName(\'div\')[0].style.display = \'none\'; this.innerText = \'\'; this.value = \'Show\'; }">
</div>
<div class="alt2" style="margin: 0px; padding: 6px; border: 1px inset;">
<div style="display: none;">
{param}
</div>
</div>
</div>

';
} else {
echo "You do not have permission for this page"; }
?>
I suggest you get a program that does syntax highlighting. For example look at your code with syntax highlighting.

<?php
if ($vbulletin->userinfo['usergroupid'] == '34'
or $vbulletin->userinfo['usergroupid'] == '6'
)
{
echo "
<div style="margin:20px; margin-top:5px">
<div class="smallfont" style="margin-bottom:2px"><b>Spoiler</b> for <i>{option}</i>: <input type="button" value="Show" style="width:45px;font-size:10px;margin:0px;padding:0px;" onClick="if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = ''; this.innerText = ''; this.value = 'Hide'; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = 'Show'; }">
</div>
<div class="alt2" style="margin: 0px; padding: 6px; border: 1px inset;">
<div style="display: none;">
{param}
</div>
</div>
</div>

";
} else {
echo "You do not have permission for this page"; }
?>
The stuff in the echo should be red to signify a string but its not.

IFeelYourPain
03-02-2008, 07:46 AM
So all you did was change the " to ' on the echo and before the ;? I tried that and it didn't work, but when you gave it to me it worked? Weird. Anyway it works, but I don't understand the whole EOF thing. If it works without it why use it?

Wait, so in the code where the ' was before you add a /?

_Aerospace_Eng_
03-02-2008, 07:54 AM
Its a \ not a / but you need to change the double quotes on the echo to single quotes or keep the double quotes on the echo and then escape the double quotes on your html but not the javascript. Please learn to search. Search on escaping quotes in strings using php.

Did you not read the link I gave you about the EOF thing? Basically it saves you time. Use heredoc syntax and you don't have to escape any quotations.

IFeelYourPain
03-02-2008, 08:11 AM
Hmm I just copied my javascript into my old echo statement and changed the " to ' and it gave me a blank page. When I take your statement and post it below my other information in an echo it works fine. Whats up with that?

_Aerospace_Eng_
03-02-2008, 02:37 PM
I keep telling you aren't escaping your quotes. Now you need to escape your single quotes. Read this: http://www.hudzilla.org/phpbook/read.php/2_6_2

digitalfiz
03-02-2008, 08:51 PM
You need to actually look at your code and read it not just copy/paste. here I'll try and give you a visual:


//See the Code Below:

echo "some string without double quotes in it";
// Looks fine right?


// Now lets do it like you did:
echo "some string with a double quote like so " and we continue see
how the text color has change?";


// Now lets do it like you did but delimiting the quote:
echo "some string with a double quote like so \" and we continue see
how the text color doesn't change?";
you have to do the same thing with single quotes too. You can mix quotes fine but when you have the same type of quote you have to delimit them. Hope this helps with your understanding of quotes.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum