0

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.

Edited by diafol: reformatted code

2
Contributors
1
Reply
14
Views
3 Years
Discussion Span
Last Post by diafol
1

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.

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.