SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    php in external javascript II

    My developer tool says there's a parse error in this line, but I don't see it. Do you?
    PHP Code:
    echo 'var div = document.getElementById('subcat');'
    Here's the function in which that line belongs
    PHP Code:
    <?php 
    session_start
    ();
        
    $url $_SESSION['url'];
        
    //$url = "http://localhost";
    session_write_close();

    header('Content-type: text/javascript');
    echo 
    'var url = "' $url '";';
    //echo 'alert(url);';
    echo 'function subcat5() {;';
            
    //echo 'alert(url + "/index.php");';
        
    echo 'var div = document.getElementById('subcat');'//get the container on the page
        //echo 'div.innerHTML = '';'; //clear out the subcat
        //echo 'var new_subcat = document.createElement('form');';//create form
        //echo 'new_subcat.action = url + "/index.php";';
        //echo 'new_subcat.id = "models";';
        //echo 'div.appendChild(new_subcat);';
    echo '};';
    ?>

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    got it: echo 'var div = document.getElementById(\'subcat\');';

    It's preferred though to stick with single quotes for javascript, and double quotes for html content.
    So, I would go with this modification instead, where double quotes are used to denote the string, so that the single quotes can be left unmolested inside of it. That way the escaped quotes don't end up causing more confusion:

    PHP Code:
    echo "var div = document.getElementById('subcat');"
    Last edited by paul_wilkins; Oct 3, 2012 at 00:40.


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
  •