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.

Dani AI

Generated

The easiest, safest flow is: keep the checkboxes inside a real <form> with name="chk_list[]", submit the form (or send it with AJAX), then handle the received array on the server with validation and a prepared statement. and already pointed to submitting and looping over the posted array; below is an example that keeps those ideas but shows a modern, secure insertion approach and a minimal fetch-based submit.

Server-side: validate, cast values to integers, then use PDO prepared statements inside a transaction so a partial failure does not leave the DB inconsistent.

<?php
if (!empty($_POST['chk_list']) && is_array($_POST['chk_list'])) {
    $ids = array_map('intval', $_POST['chk_list']);         // sanitize
    $pdo = new PDO('mysql:host=localhost;dbname=your_db;charset=utf8mb4','dbuser','dbpass',
                   [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
    $pdo->beginTransaction();
    $stmt = $pdo->prepare("INSERT INTO selected_vehicles (veh_id, created_at) VALUES (:vid, NOW())");
    foreach ($ids as $vid) { $stmt->execute([':vid' => $vid]); }
    $pdo->commit();
}
?>

Client-side (optional): submit the form with fetch to keep UX snappy and still let PHP receive the same array.

document.querySelector('form').addEventListener('submit', function(e){
  e.preventDefault();
  fetch(this.action, { method: this.method || 'post', body: new FormData(this) })
    .then(res => res.text()).then(console.log).catch(console.error);
});

Practical notes and troubleshooting:

  • In ’s markup avoid using a closing </input> tag (input is self-closing). Browsers tolerate it but it is invalid HTML.
  • Ensure the checkboxes are inside the <form> and that the form method matches what PHP reads ($_POST vs $_GET).
  • Always validate/sanitize on the server (arraymap('intval', ...) above) and avoid deprecated mysql* functions; migrate to PDO or mysqli.
  • Use print_r($_POST) (as suggested) when debugging to confirm what was submitted, and log DB errors or exceptions so failures are visible.

Recommended Answers

All 3 Replies

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.