Click here to Skip to main content
15,896,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I've been adding to an existing code which I have the task of adding checkboxes to each row fetched from MySQL database. The problem is that $_POST is returning a null when I tell it to fetch checked boxes.

Here's the checkbox creation loop:
<pre lang="PHP">
      $RowCt = 0; //Row Counter
                while($Row = mysql_fetch_assoc($Result))
                {
                   
                    $k = $RowCt-1;
                    $Table.= "<td><input id=\"chkbx".$RowCt."\" onclick=\"bindCheckbox(".$RowCt.", ".$idValues[$k].")\" type=\"checkbox\" name='chkbx[]' value=".$k.">";

                    //Loop thru each field
                    foreach($Row as $field => $value)
                    {

                        $Table.= "<td nowrap=\"nowrap\">$value</td>";

                    }
                    $Table.= "</tr>";
                }


and the code where I want to get the checked values:
PHP
$doctorIds = $_POST['chkbx'];
		//echo $doctorname;
		//$q=select("SELECT * FROM `doctor` WHERE name = '$doctorname'");
		//$doctorname=$q[0]['id'];
		//echo $doctorname;	
		if (isset($doctorIds)||is_array($doctorIds)) {	
		foreach ($doctorIds as $key => $i){
		$b=execute_sql("INSERT INTO `doctorseller`(`doctorid`, `sellerid`) VALUES ('$i','$sellerid')");
		}



I have another form element which i get using the same $_POST method but this is giving me nulls. I would appreciate any suggestions
Posted
Comments
Mohibur Rashid 12-Nov-15 19:19pm    
Not clear about your question but your $k is always -1, In loop you have </tr> but you don't have any $lt;tr$gt;.

Along with this also read about SQL Injection.
Member 12133940 13-Nov-15 8:12am    
I am aware of the SQL injection, but all I want right now is a quick and dirty solution to fix the null array I get instead the list of checked boxes.
As for the $k the counter starts at 1 hence the decrement to make it start at zero.
W Balboos, GHB 13-Nov-15 7:05am    
I'm always willing to learn. What does this mean:
<pre>
foreach($Row as $field => $value)
</pre>
Member 12133940 13-Nov-15 8:28am    
$Row is the array of rows fetched from the database. the values are the content of each field in each row, this is just the person who wrote this code did to fill the table.
W Balboos, GHB 13-Nov-15 8:31am    
I use the foreach with SQL return virtually daily - and thus know how it maps. My code would look more like:

foreach($recordSet as $r)

What I've never seen was the "=> $anotherVal" notation which follows inside the parenthesis and thus don't know what it does.

1 solution

You will have to do the following.

1. You are using a variable name $RowCt put its value in a hidden field.
2. Retrieve the value of the hidden field in the php code.
3. Run a for loop with basing on the $RowCt and use it to retrieve the checkbox values
$doctorIds = $_POST['chkbx'.$RowCt];
The above code will give the value of the individual checkboxes.

Hope the above step helps you solve your problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900