Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6914063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:17:58+00:00 2026-05-27T09:17:58+00:00

Is it possible to run javascript code within a php loop? the javascript works

  • 0

Is it possible to run javascript code within a php loop?
the javascript works perfect the problem is that it is currently not executing more than once.

while(...) {

 $l=$l+1;
 $linha="#x".$l;
 $linha2="x".$l;
  ?>

 <script type="text/javascript">
 $(document).ready(function () {

var mensagem = "<?= $mensagem ?>";
var id= "<?= $linha ?>";

var nextMsgOptions = {
    msg:  mensagem,
    side: "bottomMiddle",       
    CSSClass: "nextMsg-LightTheme",}

$(id).click(function(){
    $(id).nextMsg(nextMsgOptions);
});


});
 </script>

}

any ideas? 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:17:59+00:00Added an answer on May 27, 2026 at 9:17 am

    Yes you can output javascript within a PHP loop, to be executed by the browser, when the page loads.

    The problem here is that you’ve got tons of variables colliding. You need to encapsulate each output of that script tag in order to keep that from happening. Here’s one suggestion:

    <script type="text/javascript">
    <?php while(...): $l++; $linha = "#x" . $l; $linha2 = "x" . $l; ?>
        (function($)
        {
            var mensagem = "<?= $mensagem; ?>",
            id = "<?= $linha; ?>",
            nextMsgOptions = {
            msg: mensagem,
                side: "bottomMiddle",
                CSSClass: "nextMsg-LightTheme"
            };
    
            $(document).ready(function() {
                $(id).click(function(){
                    $(id).nextMsg(nextMsgOptions);
                });
            });
        })(jQuery);
    <? endwhile; ?>
    </script>
    

    This keeps each $linha and $mensagem variable scoped away from each other for each iteration of the loop. What I think is/was happening is in your old code, you’d set $linha to some variable, and output id = <?= $linha; ?> however many times your loop executed. When $(document).ready() executed for each output, $linha had already been interpreted to be the last value that you loop output. This caused document.ready to attach an event N times (N = number of iterations of your while loop) to the same DOM element (whichever the last iteration of your while loop output $linha to be). With the above snippet, it keeps each id and $linha variable scoped away and private from each other, so you shouldn’t have to worry about collisions.

    I realize that explanation is kind of convoluted and might be hard to grok; but javascript interpretation/execution/scoping has special rules that aren’t incredibly simple to convey without examples.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to run both debuggers within the same PHP installation simultaneously. They
I have a bit of JavaScript code that is specified in a configuration file
Is there a way to not run a Javascript function until after a custom
I've written a bit of code in JavaScript that uses the window.navigator.language and the
I'm using javascript and jQuery. I have a problem that my web app has
I want to have horizontal lists that can run as wide as possible but
I'd like to execute JavaScript code from within a C# assembly and have the
Is it possible from a Javascript code to find out where it came from?
I run Ruby/RSpec+Selenium. During the tests, the JavaScript code prints some info to the
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.