PHP variable as part of jQuery function not working, but plain text

I have the following jQuery code in my PHP file (edited January 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 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; ?>

This page is available here . Click-to-claim links do not work (this is my problem).

When I look at the source code, the PHP variables seem to repeat correctly inside jQuery:

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

It looks right, but nothing happens when I click on any of the links. However , when I replace the PHP echo operators with equal numbers (0, 1, etc.), the click functions work as expected .

: for? , - > PHP script. - "" , script - > "".

, PHP ? - (, ), , PHP JavaScript ?

+3
4

HTML-, .

-, , , , , , . , . a_0, a_1 .. d_0, d_1 .. id :

<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>

PHP jQuery :

$(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();
   });
});

a approve, id, a_. , id a_0 = 0 , confirm-approval .

+5

javascript , script PHP , , - ...

PHP echo (0, 1 ..) .

. , , , PHP.

+2

, jQuery. click() PHP- . id, .

0

- script

$(document).ready(function()

in a loop. I do not know if this caused any problems, but it is not very effective, just once.

0
source

Source: https://habr.com/ru/post/1728871/


All Articles