Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: javascript php quotes

  1. #1
    Join Date
    Aug 2006
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default javascript php quotes

    How would I do something like this?

    PHP Code:
    <?php

    echo "<script type="text/javascript">
      function openAlert() {
       Dialog.alert("
    Add your <b>HTML</bmessage here", {windowParameters: {className: "alphacube"}})
      }
      
    </script>"
    ;

    ?>
    Baiscally trying to use quotes in javascript to work with php.

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Either escape the quotes with a backslash ( \ ) or use single quotes ( ' ).

    An example:

    Code:
    <?php
    
    echo "<script type=\"text/javascript\">
      function openAlert() {
       Dialog.alert(\"Add your <b>HTML</b> message here\", {windowParameters: {className: \"alphacube\"}})
      }
      
    </script>";
    
    ?>
    Notice the backslashes.

    Hope this helps.

  3. #3
    Join Date
    Aug 2006
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    so would this work?
    PHP Code:
    echo "    <script type=\"text/javascript\" src="/javascripts/prototype.js\"> </script>
    "


  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Only if you did like this:

    Code:
    echo "    <script type=\"text/javascript\" src=\"/javascripts/prototype.js\"> </script>
    ";
    Notice the slashes in red.

    Hope this helps.

    Added Later: Just some FYI, if you have an echo with double quotes around a string ( " ), it would be wise to use single quotes in the string if needed (like in the above snippet, quotes are needed). Otherwise, backslashes to escape the quotes inside quotes would do just as well.
    Last edited by thetestingsite; 02-19-2007 at 03:30 AM.

  5. #5
    Join Date
    Aug 2006
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    im getting an error:
    Parse error: syntax error, unexpected T_STRING in /home/public_html/config.php

    heres my code:
    PHP Code:

    $systemMessageStart 
    "<BR>"<script type=\"text/javascript\">
      function openAlert() {
       Dialog.alert(\""
    ;

    $systemMessageEnd   "\", {windowParameters: {className: \"alphacube\"}})
      }
      
    </script>"


  6. #6
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Code:
    <?php
    
    echo "<script type=\"text/javascript\">function openAlert() { Dialog.alert(\"Add your <b>HTML</b> message here\", {windowParameters: {className: \"alphacube\"}})}</script>";
    
    ?>
    The above mentioned solution is based on the code that you had pasted in your first posting. If you want to assign this script tag line into some variable then not much change in the syntax, have a look at the following item

    Code:
    $scriptLine = "<script type=\"text/javascript\">function openAlert() { Dialog.alert(\"Add your <b>HTML</b> message here\", {windowParameters: {className: \"alphacube\"}})}</script>";
    or

    Code:
    $scriptLine = "<script type='text/javascript'>function openAlert() { Dialog.alert('Add your <b>HTML</b> message here', {windowParameters: {className: 'alphacube'}})}</script>";
    I personally prefer the first notation as it clearly distinguish items with little difficulty compared to the other method

  7. #7
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Be careful when adding JavaScript events to objects via PHP. See here:
    Code:
    <div onclick="<?php if ($condition) {echo "openAlert('argument1','argment2')";}; ?>"
    Must be single quotes. It would be trying to put double-quotes inside double-quotes.
    Code:
    <div onclick="<?php if ($condition) {echo "openAlert(\"argument1\",\"argment2\")";}; ?>"
    would be invalid.
    - Mike

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It's much easier to break out of PHP parsing mode:
    Code:
    function displaySystemMessage($msg) {
    ?>
      <script type="text/javascript"> 
        function openAlert() { 
          Dialog.alert(
            "<?php echo($msg); ?>",
            { 'windowParameters' : { 'className' : "alphacube" } }
          );
        }
      </script>
    <?php
    }
    Wherever you output HTML in any form, always break out of PHP parsing mode if possible. It results in code that's much easier to read and debug, increases the efficiency of your script, and avoids having to struggle with escaping quotes.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Aug 2006
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    heres my code:
    PHP Code:


    $systemMessageStart = ?><BR><script type="text/javascript">
      function openAlert() {
       Dialog.alert(
    <?php

    $systemMessageEnd   
    ?>", {windowParameters: {className: "alphacube"}})
      }
      
    </script><?php
    what am i missing? I get an error.

  10. #10
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    What's the error? The only thing I can see that's wrong with the code you posted is the ending <?php that either doesn't end, or should not be there at all. Post the error message though, it could be something else.

    Hope this helps.

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
  •