Hello Friends,

I have checkbox which is generated dynamically. Here the code is :

<?php	
	$i=0;
	while($row1 = mysql_fetch_array($result))
	 {?>
		<label><?php echo $i++ ?><input type="checkbox" name="chk_list[]"  value="<?php echo $row1['Veh_id'];?>" class="txt" onClick=""><?php echo $row1['Veh_nm']; ?></input></label></br>
	 		
	<?php  } 
		
		
		
		?>

Now, I want to insert IDs and related information which checkboxes are checked. So, I tried small javascript on button when its clicked.

function validate()
		{
			
			
			var chk = document.frm.chk_list;
			//alert (chk);
			for(i=0; i<chk.length; i++)
			if(chk[i].checked==true){
			alert (chk[i].value);
			}			
			
		}

now thing is how can I access this value in PHP? Is there any trick to pass array from javascript and use them in PHP code??

Thanking You,
Hakoo Desai.

if you have placed checkbox chk_list[] in the html <form> element, then you can submit the form with action=some.php.

Now in your php file
you will get array of selected values
$_POST[0]
$_POST[1]
$_POST[2]
.
.
.
.
$_POST[n]

Here n is number of selected check boxes,
Here Unselected checkboxes are not available in the array.

You can also use loop for getting its value.

<?
	$chk_list = $_POST['chk_list'];
	foreach($chk_list as $key=>$Veh_id)
	{
		echo '<br />'.$Veh_id.' is checked';
	}
?>

Add this code.
This code will print all forms posted data.

if(isset($_POST))
{
    echo '<pre>';
    print_r($_POST);
}

Post your output here.

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.