SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    Level 8 Chinese guy Archbob's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere in this vast universe
    Posts
    3,741
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Another Javascript/PHP syntax question

    I hate wrapping javascript within PHP, I really do. THe syntax never comes out right.

    So I have this bit in javascript:

    Code:
    <h1 onMouseOver="this.style.cursor='pointer'" onclick="window.parent.setView('$getsteps3[setview]'); window.parent.setTimeout("sendToPDF('$getsteps3[stepcalled]','$getsteps3[description]')",5000)">Step $getsteps3[stepcalled]</h1>
    In regular javascript

    How do I convert it into PHP without break functionality. THe trouble is there are double quotes inside of double quotes. I've tried single slashes, double slashes, quotes and slashes, and alot of other combinations, but none have worked right. Does anyone know.

    Thanks in advance.

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're echo'ing the string then you should escape the double quote like so:

    Code PHP:
    echo "<h1 onMouseOver=\"this.style.cursor='pointer'\" onclick=\"window.parent.setView('$getsteps3[setview]'); window.parent.setTimeout(\"sendToPDF('$getsteps3[stepcalled]','$getsteps3[description]')\",5000)\">Step $getsteps3[stepcalled]</h1>";

    Or you could leave it as it is and do this (if theres a condition):

    Code PHP:
    <?php if(/*WHATEVER*/) { ?>
    <h1 onMouseOver="this.style.cursor='pointer'" onclick="window.parent.setView('$getsteps3[setview]'); window.parent.setTimeout("sendToPDF('$getsteps3[stepcalled]','$getsteps3[description]')",5000)">Step $getsteps3[stepcalled]</h1>
    <?php } ?>
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  3. #3
    Level 8 Chinese guy Archbob's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere in this vast universe
    Posts
    3,741
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need to print the php variables within the javascript, thats why I need it in the PHP.

  4. #4
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, try this... :

    Code PHP:
    echo "<h1 onMouseOver=\"this.style.cursor='pointer'\" onclick=\"window.parent.setView('".$getsteps3[setview]."'); window.parent.setTimeout(\"sendToPDF('".$getsteps3[stepcalled]."','".$getsteps3[description]."')\",5000)\">Step ".$getsteps3[stepcalled]."</h1>";
    Last edited by JimmyP; May 27, 2008 at 13:51.
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  5. #5
    Level 8 Chinese guy Archbob's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere in this vast universe
    Posts
    3,741
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, almost, but not quite it gives this error in the FF error console:

    Code:
    Error: syntax error
    
    Line: 1, Column: 52
    Source Code:
    window.parent.setView('1'); window.parent.setTimeout(
    With an arrow pointing to the parenthesis.

  6. #6
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry Archbob! ..

    What about this? -

    Code:
    echo "<h1 onMouseOver=\"this.style.cursor='pointer'\" onclick=\"window.parent.setView('".$getsteps3[setview]."'); window.parent.setTimeout('sendToPDF('".$getsteps3[stepcalled]."','".$getsteps3[description]."')',5000)\">Step ".$getsteps3[stepcalled]."</h1>";
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  7. #7
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow. That may be the ugliest line of code I've seen this week. Why on earth are you adding this inline? Why not external js? Would be much cleaner.

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This works.

    Code:
    echo "<a href=\"#\" onClick=\"setTimeout('sendToPDF(\\'".$getsteps3[stepcalled]."\\', \\'\\')', 1000);\">click</a>";
    But I agree with the above poster, unobtrusive javascript is better if possible.

  9. #9
    Level 8 Chinese guy Archbob's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere in this vast universe
    Posts
    3,741
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hasn't worked yet.

    Logic, how does your last piece of code work with the setview() function coming first?

  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    How I would write the original line in PHP is as follows, using heredoc syntax.
    Normally php variables can be used as is, but with arrays and objects, you have to enclose them in braces.

    Code php:
    $output = <<< EOT
    <h1 onMouseOver="this.style.cursor='pointer'"
        onclick="window.parent.setView('{$getsteps3[setview]}');
            window.parent.setTimeout(
                "sendToPDF('{$getsteps3[stepcalled]}',
                '{$getsteps3[description]}')",5000
            )">Step {$getsteps3[stepcalled]}</h1>
    EOT;

    It doesn't have to be on multiple lines, but it sure helps with the readability.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  11. #11
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Archbob View Post
    Hasn't worked yet.

    Logic, how does your last piece of code work with the setview() function coming first?
    Wouldn't this work?
    onclick=\"window.parent.setView('$getsteps3[setview]');

    or

    onclick=\"window.parent.setView('".$getsteps3[setview]."');

    You should try using pmw57's example and avoid this syntactical mess.

  12. #12
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I would like to add my vote for putting that in an external JS file. It will make your application more modular, easier to maintain and it will make your PHP and HTML cleaner and easier to read. Not to mention making your HTML pages smaller and faster to load.


Bookmarks

Posting Permissions

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