I have a code in php that is being echoed in html:

 $tblAccounts .= "<td width=\"100\"><input title=\"$objRow->AccountCode\" name=\"chkAcctField[$objRow->AccountCode]\" id=\"chkAcctField[$objRow->Description]\" value=\"chkAcctField[$objRow->AccountCode]\" type=\"checkbox\" $strIsChecked>" . ucwords($objRow->Description) . "</td>";

now may problem is this...

I don't know how will I get the value of the checkboxes clicked by the user....
when the names and ids are varying. And I need to get the values using javascript....
using getElement returns null.

Please help.

Member Avatar for diafol

You could provide a classname for javascript to pick up and a array name for the php to pick up (as you've done):

$tblAccounts .= "<td width=\"100\"><input title= \"$objRow->AccountCode\" class= \"accounts\"  name=\"chkAcctField[$objRow->AccountCode]\" id=\"$objRow->AccountCode\" type=\"checkbox\" $strIsChecked>" . ucwords($objRow->Description) . "</td>";

The id field should NOT be an array item - use a simple id that can link to your DB id - if using a DB. In addition, there's no need to escape double quotes, you can use single quoted for property/attribute values in html - it seems for readable to me:

$tblAccounts .= "<td width='100'><input title= '$objRow->AccountCode' class= 'accounts'  name='chkAcctField[$objRow->AccountCode]' id='$objRow->AccountCode' type='checkbox' $strIsChecked>" . ucwords($objRow->Description) . "</td>";

if using jQuery:

$('.accounts').click(function(){
    var id = $(this).attr('id');
    alert(id);
})

php:

if($_POST)
{
    if(isset($_POST['chkAcctField']))
    {
        $chk = (array) $_POST['chkAcctField'];
        $ids = array_keys($chk);
        print_r($ids); //output all ids checked
    }
}

It should be noted that all input variables are suspect and that you should validate them all - including id values - as forms can be spoofed or data passed via cURL etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.