-2

this jquery script not work when im insert php tag into this,.. can i know what is the problem..

<script language="javascript">

    $("#openmsg_<?php echo $id; ?>").click(function(){
        $("#toggleText_<?php echo $id; ?>").toggle(800);
    })

</script>

this is my jquery code to toggle

<form action="my_messages.php" method="POST" name="<?php echo $msg_title; ?>">
<input type="button" name="openmsg" id="openmsg_<?php echo $id; ?>" value="<?php echo $msg_title; ?>" onClick="javascript:toggle<?php echo $id; ?>();" />
<input type="submit" name="setopened_<?php echo $id; ?>" value="I have read this" />
</form>

<div id="toggleText_<?php echo $id; ?>" style="display:none;">
<?php echo $msg_body; ?>
</div>
hansi
  • 1
  • 1
  • 1
    what is `$id` where is it defined? – Andy Holmes Jul 18 '14 at 10:41
  • can't do that, php codes only work in ".php" files, the way to go is to put the values that are inserted by php inside an `` and then get the value from that inside jQuery. – Amin Jafari Jul 18 '14 at 10:43
  • Are you aware of the differences between client-side and server-side code? You are not expecting $id to use a javascript variable are you? – musefan Jul 18 '14 at 10:43
  • i know how it do with javascript but i wnt to know how to creat with j query? :/ in inspect eliment of page showin id correctly but toggle not work ... – hansi Jul 18 '14 at 10:49
  • I see nothing wrong with the code, can you provide us with a link or something, so that we can check it out? – Hristo Valkanov Jul 18 '14 at 10:52
  • sorry i connot do it brother.. there have lot of pages ,, i wnt to know that is it posible to join php code with jquery like this script – hansi Jul 18 '14 at 10:59
  • Why not use classes to attach click handler? –  Jul 18 '14 at 13:12
  • You have attached a handler to the "openmsg_" element's click event twice... both with jQuery and within the html tag. Is this right? – Luke Jul 21 '14 at 09:30
  • Also, try defining your click handler inside `$(document).ready()`. It is possible the DOM has not finished loading when that script is run and so there is no such element at the time to hook the handler to. – Luke Jul 21 '14 at 09:32

2 Answers2

0

Update your code like this :

$("#openmsg_"+).click(function(){ $("#toggleTextuserid_"+).toggle(800); })
Ajit J
  • 1
0

Try this:

<script language="javascript">

var id = <?php echo $id?>;

    $("#openmsg_" + id + "").click(function(){
        $("#toggleTextuserid_" + id + "").toggle(800);
    })

</script>