#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2008
    Posts
    662
    Rep Power
    38

    PHP and javaScript


    OK, I'm trying to dynamically create javascript using a "for" loop with PHP.
    My JS and PHP looks like this:
    Code:
    <script type="text/javascript">
    
    $(document).ready(function(){        
          
            <?php
            for ($i=0; $i<$num_results; $i++)
    				{
    					$row = mysql_fetch_array($result_archives);
    					echo "  $('#";
    					echo stripslashes($row['id']);
    					echo "').click(function(){ 
    						$('#article')load('news-archives_GET.php?id=";
    					echo stripslashes($row['id']);
    					echo "');";
    					echo "return false;";
    					echo "});";
    					
    				};
    ?>					
    });
    
    </script>

    The issue is that the PHP is inside a "<script type="text/javascript">" tag, and therefore doesn't render as PHP.

    How Can I dynamically write JavaScript based on PHP variables that come from a database?

    What I'm trying to do is present article headlines. The headlines are clickable, and each one retrieves an article from a PHP page depending this article's "id". So the JS needs to account for various ID's, wihch will change over time.

    Let me know if you have any questions or need more info.
    thanks
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by lelales
    The issue is that the PHP is inside a "<script type="text/javascript">" tag, and therefore doesn't render as PHP.
    What? The PHP interpreter doesn't care where the PHP sections are. It doesn't even understand HTML.

    To the interpreter, your script looks like this:

    Code:
    yada yada yada
    
    <?php (PHP code, execute this!) ?>
    
    yada yada yada
    
    <?php (PHP code, execute this!) ?>
    
    yada yada yada
    
    ...
    It doesn't matter what the stuff outside the PHP tags says. Could be HTML, could be JavaScript, could be a text by Shakespeare. The PHP interpreter doesn't know and doesn't care. All it does is execute the code in the PHP tags.

    So whatever the problem is, it has nothing to do with script tags. I guess you messed up the query (or forgot to define $num_results), because this piece of code does what it's supposed to do. Echoing the raw $row['id'] without any escaping is something I certainly wouldn't do, but I guess PHP programmers like to play with fire.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,938
    Rep Power
    4033
    Originally Posted by lelales
    ... So the JS needs to account for various ID's, wihch will change over time.
    No it doesn't. Just re-design your JS so it is not attached to specific headlines. For example:
    Code:
    Use PHP Here to generate the HTML for your headlines.
    <ul id="headlines">
      <li><a href="news-archives_GET.php?id=XXX1">Headline 1</a></li>
      <li><a href="news-archives_GET.php?id=XXX2">Headline 2</a></li>
    </ul>
    Code:
    $(function(){
      $('#headlines').on('click', 'li', function(e){
         //This will run whenever one of the LIs is clicked
    
         //Stop event to prevent navigation if they clicked the link
         e.preventDefault();
         e.stopPropagation();
    
         //Get the links href value
         var href = $(this).find('a').attr('href');
        
         //Load the href into the articles div
         $('#article').load(href);
      });
    });
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2008
    Posts
    662
    Rep Power
    38

    thanks guys


    I the meantime, I found a work around . . . but I will bookmark and be referencing this post in the near future.

    Thanks so much!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2008
    Posts
    662
    Rep Power
    38

    OK, this seems like a JS issue


    I posted again, but I'm deleting the post and re-posting in the JavaScript section.

    Thanks again for the help thus far.
    Last edited by lelales; August 19th, 2013 at 06:42 AM. Reason: Js question
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,974
    Rep Power
    375
    there was a problem with how you are calling

    Code:
    $('.headlines').on('click', function(e){
                                    //This will run whenever one of the LIs is clicked
                               
                                    //Stop event to prevent navigation if they clicked the link
                                    e.preventDefault();
                                    e.stopPropagation();
                               
                                    //Get the links href value
                                    var href = $(this).attr('href');
                                   
                                    //Load the href into the articles div
                                    $('#article').load(href);
                                    
                                    return false;
                                 });
    Code:
    <a class="headlines" class="header_brown" href="email.html">This is article number 6 headline</a>
    notice i am using headline as a class not ID..
    Last edited by paulh1983; August 19th, 2013 at 06:40 AM.

IMN logo majestic logo threadwatch logo seochat tools logo