0

Hi i have read this thread about using php variable in jquery but somehow it doesnt work on my site: LINK

Javascript:

<script>
var row_id="<?php echo $r['id']; ?>";

$(document).ready(function(){
  $("#que"+row_id).click(function(){
    $("#ans"+row_id).slideToggle();
  });
});


</script>
<?php foreach($res as $r) : ?>

    <li id="que<?php echo $r['id']; ?>">Q<?php echo $r['id']; ?>: <?php echo $r['question']; ?>          </li> 
    <div id="ans<?php echo $r['id']; ?>" style="padding:5px;">A: <?php echo $r['answer']; ?></div> 
<?php endforeach; ?>
Community
  • 1
  • 1
answerway
  • 11
  • 2

5 Answers5

7

Just use classes. Otherwise you will end up having a jquery piece of code for each question.

<script>
$(document).ready(function(){
  $(".que").click(function(){
    $(this).next().slideToggle();
  });
});
</script>

<?php foreach($res as $r) : ?>
    <li class="que">Q<?php echo $r['id']; ?>: <?php echo $r['question']; ?></li> 
    <div style="padding:5px;">A: <?php echo $r['answer']; ?></div> 
<?php endforeach; ?>

FIDDLE

Khawer Zeshan
  • 9,470
  • 6
  • 40
  • 63
2

Script tag should be inside the forach loop.

Please try this and explain exact what error you got?

Maulik patel
  • 1,551
  • 8
  • 22
  • 44
0

you wouldn't get this value var row_id="<?php echo $r['id']; ?>"; because $r['id'] is in the foreach.

I assume that you want to take the $r['id'], so try something like this

<script>
    $(document).ready(function(){
        $(".row").click(function(){
            var row_id = $(this).val();
            $("#ans"+row_id).slideToggle();
        });
    });

</script>

<?php foreach($res as $r) : ?>

    <li id="que<?php echo $r['id']; ?>">Q<?php echo $r['id']; ?>: <?php echo $r['question']; ?></li>
    <input type="hidden" value="<?php echo $r['id']; ?>" class="row" />
    <div id="ans<?php echo $r['id']; ?>" style="padding:5px;">A: <?php echo $r['answer']; ?></div>
<?php endforeach; ?>
sas
  • 2,563
  • 1
  • 20
  • 28
0

Use classes and .next() to solve this easily:

<script>

$(document).ready(function(){
  $('.que').click(function(){
    $(this).next('.ans').slideToggle();
  });
});


</script>
<?php foreach($res as $r) : ?>
    <li id="que<?php echo $r['id']; ?>" class="que">Q<?php echo $r['id']; ?>: <?php echo $r['question']; ?>          </li> 
    <div id="ans<?php echo $r['id']; ?>" class="ans" style="padding:5px;">A: <?php echo $r['answer']; ?></div> 
<?php endforeach; ?>
Deryck
  • 7,608
  • 2
  • 24
  • 43
-1

I think you have used the undefined variable $r in javascript.

Which is only defined in the foreach loop. get the javascript into the loop it will work.

<script>
    function q_click(row_id){
        $("#ans"+row_id).slideToggle();
    }
 </script>
    <?php foreach($res as $r) : ?>

        <li id="que<?php echo $r['id']; ?>">Q<?php echo $r['id']; ?>: <?php echo $r['question']; ? onclick="q_click(<?php echo $r['id']; ?>)">          </li> 
        <div id="ans<?php echo $r['id']; ?>" style="padding:5px;">A: <?php echo $r['answer']; ?></div> 
    <?php endforeach; ?>
Mohan
  • 143
  • 10
  • I can't see `$r` anywhere in the javascript context in the asker's code. – John Dvorak Jan 02 '14 at 07:41
  • $r is as PHP variable which is used in javascript – Mohan Jan 02 '14 at 07:42
  • Concerning your code - I don't think it's a good idea to use a separate `$(document).ready(...)` call for each row in the table. Perhaps you should loop twice instead? (or just see [@Khawer's answer](http://stackoverflow.com/a/20878440/499214)) – John Dvorak Jan 02 '14 at 07:45
  • @JanDvorak any relation to John C. Dvorak? lol and for the author of this answer: This is very wasteful and inefficient code. Please reconsider – Deryck Jan 02 '14 at 07:48
  • This is not best to use but it should work. @khawer gives the best solution for this. – Mohan Jan 02 '14 at 07:48
  • @JanDvorak I have edited my answer and now it should work efficiently. – Mohan Jan 02 '14 at 07:58