Difficulties with form action and unique identifiers
I have this code in foreach that lists uniquecode links:
<a id="<?php echo $id_to ?>" 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" 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>
</form>
this is what i get when i look at the page source:
<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" 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>
</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" 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>
</form>
the problem is when the action fires and the page goes to messageSent and I look at the page source again. $id_toIs NOT the identifier of the link I clicked on. It takes the first link identifier no matter which link I click on?
here's the messageSent source (I clicked the link with id 38 NOT 36): where I have print_r($_REQUEST)it and it gives:
Array
(
[id] => 36
[message] => bj,nbgj,
[Submit] => Send
)
.
function showMessageArea(link)
{
var message_area = document.getElementById('message_area');
message_area.parentNode.removeChild(message_area);
link.parentNode.insertBefore(message_area, link.nextSibling);
message_area.style.display="block";
}
The problem is really unique identifiers.
$to_id , (, <form id="message_area_<?php echo $to_id; ?>" ...).
showMessageArea, :
var message_area = document.getElementById('message_area_'+this.id);
, .
id <input type='hidden' name='id' id='message_id' value=''> showMessageArea(...) :
document.getElementById('message_id').value = this.id;
, , , :
<a id="<?php echo $id_to ?>" href="<?php echo ADDRESS; ?>messageSent.php 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" 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 type="hidden" name="id" value="<?php echo $id_to ?>" />
<input name="Submit" type="submit" value="Send" />
</form>
print_r($_POST)
EDIT:
, Helloise, , :
<a id="messageid_<?php echo $id_to ?>" href="javascript:void(0);" class="charcoal_link" style="line-height: 20px;" onclick="showMessageArea(this,'<?php echo $id_to ?>'); return false;" >
<?php echo $uniqueCode1?><span class="pink_text"><?php echo $uniqueCode2?></span><?php echo $uniqueCode3?>
</a>
<form id="message_area" style="display:none" method="post" action="<?php echo ADDRESS; ?>messageSent.php">
<textarea name="message" rows="10" cols="20"></textarea>
<input name="Submit" type="submit" value="Send" />
<input type="hidden" id="message_id" name="id" value="" />
</form>
<script type="text/javascript">
function showMessageArea(link,messageId)
{
document.getElementById('message_id').value=messageId;
message_area.parentNode.removeChild(message_area);
link.parentNode.insertBefore(message_area, link.nextSibling);
document.getElementById('message_area').style.display="block";
}
</script>
So, the main thing here is to have one form, as you stated, but pass the identifier that we use to the javascript function. From there, you can set the value of the hidden input, which will then be included in the POST values.
Try something like this:
after a quick look ...
- your missing
$upADDRESS. It should be$ADDRESS. I assume this is a variable (ignore this, skipping it only constant) your missing semicolon after many php lines.
<a id="<?php echo $id_to; ?>" 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" 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> </form>