Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    263
    Thanks
    19
    Thanked 0 Times in 0 Posts

    PHP and Javascript

    I can't make them work together. The quotations in PHP = out with the quotations in the javascript. What can I do?

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You need to escape the quotes.
    PHP Code:
    echo '<a href="test.html" onclick="dosomething(\'blah\');return false">test</a>'
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Posts
    263
    Thanks
    19
    Thanked 0 Times in 0 Posts
    I tried, but its giving me a blank page. I know it has something to do with putting ' somewhere, but don't know where:

    Code:
    <?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?

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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 Code:
    <?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"; }
        
    ?>
    Last edited by _Aerospace_Eng_; 03-02-2008 at 07:39 AM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    Regular Coder
    Join Date
    Sep 2007
    Posts
    263
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Double quotes where? Whats the EOF thing?

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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. 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 Code:
    <?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 Code:
    <?php
    if ($vbulletin->userinfo['usergroupid'] == '34'  
         
    or $vbulletin->userinfo['usergroupid'] == '6'  
    )
        {
        echo 
    "
    <div style="
    margin:20pxmargin-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="margin0pxpadding6pxborder1px inset;">
    <div style="
    displaynone;">
    {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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #7
    Regular Coder
    Join Date
    Sep 2007
    Posts
    263
    Thanks
    19
    Thanked 0 Times in 0 Posts
    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 /?
    Last edited by IFeelYourPain; 03-02-2008 at 07:49 AM.

  • #8
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #9
    Regular Coder
    Join Date
    Sep 2007
    Posts
    263
    Thanks
    19
    Thanked 0 Times in 0 Posts
    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?

  • #10
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #11
    New Coder
    Join Date
    Mar 2008
    Location
    Lakeland, FL
    Posts
    39
    Thanks
    1
    Thanked 3 Times in 3 Posts
    You need to actually look at your code and read it not just copy/paste. here I'll try and give you a visual:

    PHP Code:
    //See the Code Below:

    echo "some string without double quotes in it";
    // Looks fine right? 
    PHP Code:
    // 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
    ?"; 
    PHP Code:
    // 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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •