如何获得foreach中唯一的表单id


how to get a form id unique in a foreach

这是我的代码:

<?php foreach ($users as $user)
{
   //some php code here to define variables
   <a href="<?php echo ADDRESS; ?>messageSent.php?id=<?php echo $id_to; ?>" class="charcoal_link" style="line-height: 20px;" onclick="showMessageArea(this); return false;" >
      <?php echo $uniqueCode1;?><span class="pink_text"><?php echo $uniqueCode2;?></span><?php echo $uniqueCode3;?>
   </a>      
   <form id="message_area_<?php echo $id_to; ?>" style="display:none"  method="post" action="<?php echo ADDRESS; ?>messageSent.php?id=<?php echo $id_to; ?>"> 
       <textarea name="message" rows="10" cols="20"></textarea>
       <input name="Submit" type="submit" value="Send"></input>
       <input type='hidden' name='mid' id='message_id' value=""></input>
   </form>
 <?php
  }
 ?>

然后我有:

<script type="text/JavaScript">
function showMessageArea(link)
{
  document.getElementById('message_id').value = this.id;
  var message_area = document.getElementById('message_area_'+this.id);
  message_area.parentNode.removeChild(message_area);
  link.parentNode.insertBefore(message_area, link.nextSibling);
  message_area.style.display="block";
}

我的pagesource显示如下:注意表单id是唯一的

<a href="http://www-rainbowcode-mobi/messageSent.php?id=36" class="charcoal_link" style="line-height: 20px;" onclick="showMessageArea(this); return false;" > 
    KUZELJA<span class="pink_text">000</span>RC
</a>      
<form id="message_area_36" style="display:none"  method="post" action="http://www-rainbowcode-mobi/messageSent.php?id=36"> 
   <textarea name="message" rows="10" cols="20"></textarea> 
   <input name="Submit" type="submit" value="Send"></input> 
   <input type='hidden' name='mid' id='message_id' value=""></input> 
</form> 
<a href="http://www-rainbowcode-mobi/messageSent.php?id=38" class="charcoal_link" style="line-height: 20px;" onclick="showMessageArea(this); return false;" > 
   ALANZIM<span class="pink_text">000</span>RC 
</a>      
<form id="message_area_38" style="display:none"  method="post" action="http://www-rainbowcode-mobi/messageSent.php?id=38"> 
   <textarea name="message" rows="10" cols="20"></textarea> 
   <input name="Submit" type="submit" value="Send"></input> 
   <input type='hidden' name='mid' id='message_id' value=""></input> 
</form> 

现在的问题是:在我的JS中,this.id是未定义的?????THUS未显示我的文本区域和sumbit按钮如何使var message_area=document.getElementById('message_area_'+this.id)唯一???我还尝试拆分.link("=")[1]以获取id并将其与messagearea连接_但它不起作用链接也是唯一的,http://www-rainbowcode-mobi/messageSent.php?id=36其中id将是我在上点击的uniquecode链接的id

求你了,我需要帮助吗?感谢

有两个选项:

1) 将id="<?php echo $to_id; ?>"添加到<a>标签或

2) 更好地将id作为javascript函数参数传递:

<a ... onclick="showMessageArea(this, '<?php echo $to_id; ?>'); return false;">

然后你的javascript函数应该看起来像这个

function showMessageArea(link, id)
{
    document.getElementById('message_id').value = id;
    var message_area = document.getElementById('message_area_'+id);
    ....
}

尝试在php代码中更改此项:

onclick="showMessageArea('<?php echo $id_to; ?>'); return false;"

然后您可以通过以下方式获取消息区域:

var message_area = document.getElementById('message_area_'+link);