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

  • SEARCH
  • Home
  • 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 7648771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:48:01+00:00 2026-05-31T10:48:01+00:00

Alright, if you look at the code below, I am trying to make it

  • 0

Alright, if you look at the code below, I am trying to make it more “flexible”, moving the JavaScript code to its own file, instead of keeping it in the php file.

The code below is not gonna work anymore, since i cant transfer $yadayada['id'] to the JS file, well okay I dont know how to, and thats where my problem lies.
How can I use the code below, or some variant of it thats gonna work?

Whats gonna happen is that when you press a specific image(button), a modal will open for the specific post in the while statement.

I have skipped out the post part, since thats not the problem here, it is opening a modal window for the correct post.

Thanks in advance!

$yadaya = mysql_query("blablabal")
while($yadayada = mysql_fetch_assoc($yadaya)
{
    <div id="kommentera<?=$yadayada['id']?>" class="settingsBox" style="display: none; width:500px; font-size: 14px;">
    <textarea id="text<?=$yadayada['id']?>" class="textarea" style="width: 493px; height:80px;"></textarea><br />
    <span class="buttons" style="float:left;">
    <button id="kommenteraFilm" id1="<?=$yadayada['id']?>" uid1="<?=$yadayada['guid']?>" uid2="<?=$acc_info['id']?>" class="positive" type="submit" name="kommentera"><img src="<?=$static?>/img/bullet_go.png" alt="" />Kommentera</button>
    </span>
    <?php                                
    ?>
    </div>                                                                                          

    echo '
    <div id="se-kommentera'.$yadayada['id'].'" class="testing" style="float:right; margin-top:-2px; cursor:pointer;">
    <img src="'.$static.'/img/icon_kommentera.png" height="15px" width="15px" alt="" title="Kommentera" />
    </div>'
    ;
}

footer:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="<?=$static?>/js/mylibs/dependency.js"></script

dependency.js:

$(function() {
    $('#se-kommentera<?=$yadayada['id']?>').click(function (e) {
        $('#kommentera<?=$yadayada['id']?>').modal();

          return false;
    });    
});                                

UPDATE

To answer some of the responses, I think you are forgetting that I cannot access the while statement outside the statement itself, so making the js file a php file is kind of useless.

The JS file must be below the jQuery library file, which is in the footer.

  • 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-31T10:48:02+00:00Added an answer on May 31, 2026 at 10:48 am

    You can generate Javascript through PHP (but it’s not a good idea)

    To directly answer your question: you can “generate” Javascript dynamically through PHP code. For example:

    <!-- note that the source is .php so that the server processes it as such -->
    <script type="text/javascript" src="dependency.php"></script
    

    And dependency.php would look like:

    <?php
    // tell the browser how to interpret what we 'll be sending
    header('Content-Type: text/javascript');
    
    // get reference to any variables we need
    $yadayada = /* ... */
    ?>
    
    $(function() {
        $('#se-kommentera<?=$yadayada['id']?>').click(function (e) {
            $('#kommentera<?=$yadayada['id']?>').modal();
              return false;
        });    
    });
    

    All of this is really the same thing you are doing when outputting HTML with PHP, only now you are outputting Javascript. But the problem is that you are doing the same work (what is required to get to $yadayada) two times once for your HTML, and once for your JS.

    So what is a good idea?

    Simply change your markup so that you do not need to know anything inside $yadayada when you write your JS; this way, the code can remain static. For example:

    HTML:

    <div class="se-kommentera testing">
        <img src="..." height="15px" width="15px" alt="" title="Kommentera" />
        <div class="settingsBox kommentera">
        </div>
    </div>
    

    JS:

    $(function() {
        $('.se-kommentera').click(function (e) {
            $(this).find(".kommentera").modal();
            return false;
        });    
    });
    

    What I did here is provide a way of finding the appropriate .kommentera div not with an absolute identifier (the id) but in a manner relative to the .se-kommentera that was clicked.

    For convenience, I chose to put one div inside the other and get a hold of it with .find; but this is not binding and you could really make dozens of different choices here. For example, you can give both divs an id like you did before and then do something like this:

    $(function() {
        $('.se-kommentera').click(function (e) {
            var id = this.id.replace("se-", "");
            $("#" + id).modal();
            return false;
        });    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, to start with let's look at some code: <html> <body> <script type=text/javascript> function
Alright, I'm trying to read a comma delimited file and then put that into
Alright I'm pretty new to programming and stuff and I'm now trying to code
Alright so here is the deal. I am trying to make a application that's
Alright now i am trying to make a cool looking table with using CSS
Alright, currently I have my SWF hitting a php file that will go and
Alright, after doing a ton of research and trying almost every managed CPP Redist
Consider this code: public void actionPerformed(ActionEvent e) { setEnabled(false); new SwingWorker<File, Void>() { private
Have a look at this page: http://labs.pieterdedecker.be/hetoog/layout.htm It looks alright in Firefox, but IE
I am trying to read a file in a specific file format in c.

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.