Forum Moderators: open

Message Too Old, No Replies

Pass checkbox value to PHP

Pass checkbox array value to PHP

         

kenchix1

11:53 am on Jan 22, 2009 (gmt 0)

10+ Year Member



This part displays the checkboxes with the category and description


while (list($catid,$description)=mysql_fetch_row($result))
{
$col++;
echo "<td width='50px' align='center' id='dtl_box'>";
echo $description;
echo "<div align='right' valign='bottom'><input type='checkbox' id='catgrp' name='catid[]' value='". $catid. "'></div>";
echo "</td>";
if ($col>5) {
$col=0;
echo "</tr>";
}
}
?>
</td><tr><td colspan='6'>With Selected <input name='delcat' type='button' value='Delete' onclick="javascript:if(chkform(1)){catman(2)}"><input name='resetbutton' type='reset' value='Unselect'></form></tr><tr>
</tr></table>
<?
}

After the user selected the categories by checking the checkboxes, it will ask if he really wanted to delete the categories, then if yes proceed to catman() function.


...
if (vsrc=2) {
var cats= document.getElementById('catgrp').value;
if (cats!='') {
var queryString = "?c=" + cats;
ajaxRequest.open("GET", "killcats.php" + queryString, true);
ajaxRequest.send(null);

this is the portion where it will call killcats.php along with its query string which, I assumed is an array. BUT, there goes the problem, killcats.php can't grab the array value from where it came from and gives an error on mysql query.

any suggestion how to capture array value from the checkboxes ?

Thanks in advance.

methode

7:16 pm on Jan 22, 2009 (gmt 0)

10+ Year Member



well, transfer using POST rather than GET, then you can capture that array.
Assuming that I understood what you mean :-S

kenchix1

3:43 am on Jan 23, 2009 (gmt 0)

10+ Year Member



ajaxRequest.open("POST", "killcats.php" + queryString, true);

Still didn't work.

Thanks.

Fotiman

4:25 am on Jan 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your PHP code is generating invalid HTML. You will have multiple checkboxes all with the same id 'catgrp', but id's must be unique. Instead, maybe try getting elements by name.

kenchix1

6:16 am on Jan 23, 2009 (gmt 0)

10+ Year Member





...
var cats= document.getElementsByName('catid').value;
...


....
echo "<div align='right' valign='bottom'><input type='checkbox' name='catid[]' value='". $catid. "'></div>";
...

Here's the PHP code :


$selcats=$_REQUEST['c'];
$selcats = implode(',',$selcats);
$qry="delete from usrcategory where `catid` in ".$selcats." and uid=". $_SESSION['uid'];
$result=mysql_query($qry) or die(" may mali $qry!");

Array
(
[c] => undefined
....
)

Anybody suggestions ?

Thanks for the effort.

kenchix1

8:51 am on Jan 23, 2009 (gmt 0)

10+ Year Member



It's working now and this is what I did.

I created a javascript function that gets all the .checked from checkedboxes then put it in a string.

-----
function getCheckboxValue()
{
var catids="";
for(var el=0; el < document.catfrm.catid.length; el++)
{
if(document.catfrm.catid[el].checked)
{
catids =catids+document.catfrm.catid[el].value+',';
}
}

return catids;
}
----

then I pass the string to PHP script.

Thanks ! :)

coopster

1:36 pm on Jan 23, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



For the record ...
ajaxRequest.open("POST", "killcats.php" + queryString, true);

... passes the queryString as exactly that, a QUERY STRING. Not POST data. In order to send POST data via AJAX you need to pass the string in the send() method. And don't forget to set the request header.
ajaxRequest.send(queryString); 
ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');