我在具有不同id的while循环中有php表单。从重复循环的php表单中获取变量
<?php
while($pet = $results->fetch(PDO::FETCH_ASSOC)){
?>
<form method="post" name="petForm" id="petForm">
<ul id="listAvatars">
<li>
<input type='text' name='pet' id='pet' value='<?=$pet['PetId']?>' />
<img src="<?=PET_AVATAR.$pet['Avatar']?>" class="listAvatar">
<span onclick='setPet(<?=$pet['PetId']?>)' id="avatarName"><?=$pet['PetName']?></span>
</li>
</ul>
</form>
<?php
}
?>
我想在点击事件中获得不同的宠物ID。通过此功能,我可以在点击名称上获得有效的宠物ID。
function setPet(petId){
alert(petId);
document.getElementById("petForm").submit();
}
PHP:
if(isset($_POST['pet'])){
$petId = $_POST['pet'];
print_r($petId);
}
提交此表格,我只得到了第一个宠物的ID。我应该怎么做才能选择合适的宠物ID?
我试过了..在这种情况下,我只得到最后一个值.. –
@ user3067928更新了答案,请尝试 –
在alert(petId)中,我得到了不同的宠物ID。但是,在表单提交中,我只获得最后一个元素。 –