Trying to set checkbox value with php variable

Javascript coding ..

Moderators: egami, macek, gesf

Post Reply

Fri May 18, 2012 3:55 am

I am trying to set the value of checkboxes with the id from a mysql database.

$row[0] is the id of the database.

Code: Select all

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
and here is the code for the checkboxes.

Code: Select all

echo "<tr class=\"d".($i & 1)."\"><td align=\"left\">";
echo "<h2><font color=#000000>Visit&nbsp;<input type=\"checkbox\" class=\"chcktbl\" value=\"$row[0]\" /></font>$row[0]&nbsp;&nbsp;&nbsp;$row[6]</h2><em><strong><font color = #FF0000>Sale is on $slongdate until $elongdate.
~ Times are $mystime to $myetime</strong></em></td>";
The second $row[0] inserted after '</font>' is just to make sure that each $row[0] entered in to the table has the id from the database.
The problem is that all I get from the checkbox is a value of 1 for the $row[0].
Here's the javascript.

Code: Select all

var visitcount =0;
var mylink =0;

    $('.chcktbl').click(function () { 
        visitcount = $('.chcktbl:checked').length;  
		mylink = $('.chcktbl').val();
		alert(mylink+' '+visitcount);

The 'visitcount' works fine, but not the value for $row[0].
I've tried many combinations using single quotes, double quotes etc, but I get either 'on' suggesting no value is attached to the checkbox or 1. If I replace the $row[0] with a number then I get that number showing in the js alert box.
Anyone tell me what I am doing wrong here, please?

User avatar
php-forum Fan User
php-forum Fan User
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Fri May 18, 2012 7:02 am

How many check boxes like this are there? Is there more than one, because it seems like you are looping through multiple records.

When are you calling the javaScript? Since you are only passing the class of the check box and there are many boxes on the form, only the first one is actually grabbed on execution of this script.

If you wanted to change this, you would probably want to call this script onchange of the check box value and then just use the "this" keyword. Either that or you need to more distinctly call out which check box you are trying to get the value of.


Fri May 18, 2012 10:20 pm

Thanks for the reply Nullsig.

I am looping through a database (at this stage in test mode so only 8 entries) and placing a checkbox in each row of the table.

The javascript is called when the checkbox is clicked.

I will try the Onchange function to see if that returns the value of the checkbox that is clicked.


Fri May 18, 2012 10:57 pm

Further to my post...
What I am trying to do is to detect which records from the database the user has selected. The user can click on the checkbox called Visit to indicate the places to visit. The current javascript counts the number of checkboxes that are ticked. What I want to do is to attach the id of the record to the associated checkbox and then create an array of record ids from the checkboxes that are ticked. From this array I want to print out only the records that match the ids.

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest