[英]How to pass same class/id name php values from radio button to javascript?
This is my problem. 这是我的问题。 I am looping items from the database that contains items name and id. 我正在从包含项目名称和ID的数据库中循环项目。 I have a radio button for each items. 每个项目都有一个单选按钮。 The id of that item is stored in the radio buttons value ....Now what i want to achieve is when i click on the submit button,it will call a function call save() in javascript and save all the selected items ONLY .... i've been trying this for hours but i cant get it worked. 该项目的ID存储在单选按钮值中。现在,我要实现的是单击提交按钮时,它将在javascript中调用函数调用save()并仅保存所有选定的项目。 ...我已经尝试了几个小时,但我无法使其正常工作。
Here are my codes.... 这是我的代码...
When i do my while looping from the DB, it will display the radio button and the items name... 当我从数据库循环时,它将显示单选按钮和项目名称...
<?php
$retrieveQuery = "SELECT * FROM `itemdb` WHERE `approve`= 1 AND `userId` = '$userId';";
$retrieveresult = mysqli_query($dbconnect, $retrieveQuery);
if(mysqli_num_rows($retrieveresult) > 0)
{
while($retrieverow = mysqli_fetch_assoc($retrieveresult))
{
?>
<p> </p>
<div style="background-color: rgba(0, 0, 0, 0.16);">
<input style="margin-bottom: -19px;" type="checkbox" class="item" name="item[]" value=<?php echo $retrieverow['itemID'] ?>>
<p style="margin-left: 26px;"><?php echo $retrieverow['itemName'] ?></p>
<p style="margin-top:-10px;margin-left:28px"><i><?php echo $retrieverow['category'] ?></i></p>
</div>
<?php
}
}
else
{
echo "Test : There is no approve Items to be displayed";
echo "Either wait for the admin to approve or upload items to start Trading ! ";
}
?>
This is my script:(How can i save my items?) 这是我的脚本:(如何保存我的物品?)
function save()
{
var f = document.getElementsByClassName('item').value; //my own items chosen frm invntry
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("confirmTrade").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("POST","trade.php?f="+f,true);
xmlhttp.send();
}
Right after i click the save() function it will send the data to another page using ajax method.... 在我单击save()函数后,它将使用ajax方法将数据发送到另一页。
PS: Everything works fine...Other variables are passed successfully but only one item variable is being passed .... PS:一切正常...其他变量已成功传递, 但仅一项变量已传递 ...。
Thanks in advance! 提前致谢!
Without looking into it too much, take a look at this line: 无需过多研究,请看以下这一行:
var f = document.getElementsByClassName('item').value;
document.getElementsByClassName
gets an HTMLCollection of elements, so calling value on this doesn't make much sense. document.getElementsByClassName
获取元素的HTMLCollection ,因此对此调用值没有多大意义。 You'll need to loop through them and find the selected ones. 您需要遍历它们并找到所选的。 I have it building an array then joining it into a comma separated list: 我让它建立一个数组,然后将其加入逗号分隔的列表中:
var checkboxes = document.getElementsByClassName('item');
var f = [];
for(var i = 0; i < checkboxes.length; i++){
if(checkboxes[i].checked == true)
f.push(checkboxes[i].value);
}
f = f.join();//makes comma seperated list
//proceed with ajax call
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.