mercredi 30 mars 2016

how to check checkbox in javascript loop

I already get the id from a php loop for my checkboxes, and pass them as a string(maybe not because I could not split them with comma) in parameter, then I need to check if the checkbox is checked in javascript using the ids I passed through. It doesnt seem like I can split it in javascript as well, and after I ran the for loop, the data is undefined in the new string. Do you have any ideas? Please help

here is my php

echo "<div id='addstock'>";
$ids = '';
while($row_add = mysqli_fetch_array($result_add)){

    $id=$row_add['id'];
    $company = $row_add['companyname'];
    //create checkbox for company
    echo "<p class='checkbox'><input type='checkbox' name='stocks' id='".$id."' value='".$id."'>".$company."</p><br>";
    $ids .= $id;
}
echo "</div>"; 
echo "<p class='input'><input type='submit' class='submitbutton' value='Submit' onclick='updatetable(".$ids.",".$user.")'></p>";

here is my javascript

//update table after add to stock
function updatetable(ids,user){
var url = "update.php";
//var res= ids.split(" ");
alert(ids);
var stocks = "";
 //check if the checkbox is checked
for(var id in ids){
    if(document.getElementById(ids[id]).checked)
    {   
        stocks += ids[id];
        alert(ids[id]);
    }
}
//alert(stocks);
var data = "ids="+stocks+"&user="+user;
alert(data);
ajaxRequest(url, "POST", data, true, proceedUpdate);    
}
function proceedUpdate(response){
target_div = document.getElementById("tablediv");
target_div.innerHTML = response; 
}




Aucun commentaire:

Enregistrer un commentaire