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 555999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:52:26+00:00 2026-05-13T11:52:26+00:00

I have the following jQuery code in my PHP file (edited Jan 19 2010

  • 0

I have the following jQuery code in my PHP file (edited Jan 19 2010 @ 10:40 MST):

<?php
   $count = 0;
   foreach($attachments as $attachment) :
      echo '<script type="text/javascript">
               $(\'#a_'.$count.'\').click(function() {
                  $(\'#d_'.$count.'\').show(200);
               });

               // if "no" is clicked
               $(\'#d_'.$count.' .no\').click(function() {
                  $(\'#d_'.$count.'\').hide(200);
               });

               // if "yes" is clicked
               $(\'#d_'.$count.' .yes\').click(function() {
                  $(\'#d_'.$count.'\').hide(200);

                  // update database table -- this is why I need the script inside the for loop!
                  var jsonURL = \'http://path/to/update_db_script.php\';
                  $.getJSON(jsonURL, {\'post_id\' : '.$attachment->ID.'}, function(data) {
                     alert(\'Thank you. Your approval was received.\');
                  });
                  $(\'#a_'.$count.'\').replaceWith(\'<span>Approved</span>\');
               });
            </script>';

      echo '<li>';
         if($attachment->post_excerpt == 'approved') {
            // Check the proof's status to see if it reads "approved"
            echo '<span>Approved</span>';
         } else { ?>
            // If not yet approved, show options
            <a class="approve" id="a_<?php echo $count; ?>" href="#">Click to Approve</a>
            <div class="confirm-approval" id="d_<?php echo $count; ?>">
               <p>Please confirm that you would like to approve this proof:</p>
               <a class="yes" href="#">Yes, I approve</a>
               <a class="no" href="#">No, not yet</a>
            </div><?php
         } ?>
      </li>
   <?php $count++;
endforeach; ?>

The page in question is available here. The “click to approve” links do not work (that’s my problem).

When I view source, the PHP variables appear to have echoed properly inside the jQuery:

<script type="text/javascript">
   $('#a_0').click(function() {
      $('#d_0').show(200);
   });
   ... etc ...
</script>

This looks correct, but nothing happens when I click any of the links. However, when I replace the PHP echo statements with plain numbers (0, 1, etc.) the click functions work as expected.

You may be asking: why on earth do you have this inside a for loop? The reason is that I need to retrieve the attachment->ID variable and pass it to an external PHP script. When someone clicks “approve” and confirms, the external script takes the attachment->ID and updates a database value to read “approved”.

Why won’t the click function fire when PHP is in place? Is there some kind of greater force at work here (e.g., hosting limitation), or am I missing a fundamental piece of how PHP and JavaScript interact?

  • 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-13T11:52:26+00:00Added an answer on May 13, 2026 at 11:52 am

    Since you didn’t post your HTML its a little hard to troubleshoot.

    First, I am not sure why one is working and the other is not since the code it is outputting looks correct. Either way, I still would make some changes. Move your a_0,a_1, etc and d_0,d_1, etc into the id attribute instead of a class:

    <div><a href="#" id="a_0" class="approve">Click Me</a></div>
    <div class="confirm_approval" id="d_0">Show Me</div>
    <div><a href="#" id="a_1" class="approve">Click Me</a></div>
    <div class="confirm_approval" id="d_1">Show Me</div>
    

    Now, instead of outputting your code in a loop in PHP, place this jQuery code once on your page:

    $(document).ready(function(){
       $("a.approve[id^='a_']").click(function(e){
          var id = this.id.replace('a_',''); // Get the id for this link
          $('#d_' + id + '.confirm-approval').show(200);
          e.preventDefault();
       });
    });
    

    This code finds any a element with the approve class that has an id that starts with a_. When this is clicked, it grabs the number off the id a_0 = 0 and uses that id to find the confirm-approval element and show it.

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

Sidebar

Ask A Question

Stats

  • Questions 365k
  • Answers 365k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You'll find it with the Reference Assemblies, as sigdata and… May 14, 2026 at 3:52 pm
  • Editorial Team
    Editorial Team added an answer You can use Ruby's open classes to make your code… May 14, 2026 at 3:52 pm
  • Editorial Team
    Editorial Team added an answer Yes. It is considered a bad practice. Your HTML (if… May 14, 2026 at 3:52 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.