<form method="POST" action="prosesMultiple.php">
	<?php
	include 'connection.php';
	$sql="SELECT  defect_code,totalDaily FROM grandTotal ORDER BY totalDaily DESC LIMIT 10 ";
	$row = mysql_query ($sql);
   	while ($check=mysql_fetch_array($row)){?>
	
	   <input type="checkbox" name="defect_code[]" id="defect_code[]" value="<?php echo $check["defect_code"];?>" />
	   <?php echo $check["defect_code"];?> - <?php echo $check["totalDaily"]; ?> 
	   <input name="text"  name="totalDaily[]" id="totalDaily[]" type="hidden"  value="<?php echo $check["totalDaily"]; ?>"><br/>
   <?php } ?>
<p><input type="submit" value="Send" name="Send"></p>
</form>

the problem is i want pass the value of defect_code and totalDaily together at same time? after user select the checkboxes..is it practical i do like that

Dani AI

Generated

Short answer: keep each checkbox as the single control the user interacts with, store the paired value (code + metric) in a data attribute on that checkbox, and serialize the selected rows into one hidden field when the form is submitted. That avoids duplicate name/id problems and lets server code receive a single, predictable payload. As noted, remove duplicate name attributes; as pointed out, fix quoting when mixing PHP and HTML.

Example client-side approach (unobtrusive JS):

  • Put the defect code and total in data attributes on each checkbox.
  • Prevent more than five selections as the user clicks.
  • On submit, collect checked boxes into an array and write JSON into a hidden input.
/* attach to the page after elements exist */
document.getElementById('myForm').addEventListener('submit', function(e){
  var max = 5;
  var checked = Array.prototype.slice.call(this.querySelectorAll('input.rowPick:checked'));
  if(checked.length === 0){ e.preventDefault(); alert('Select at least one'); return; }
  if(checked.length > max){ e.preventDefault(); alert('Please select up to ' + max); return; }
  var payload = checked.map(function(cb){
    return { code: cb.getAttribute('data-code'), total: cb.getAttribute('data-total') };
  });
  document.getElementById('payload').value = JSON.stringify(payload);
});
/* optional: block extra checks as the user clicks */
document.getElementById('myForm').addEventListener('click', function(e){
  if(e.target && e.target.classList && e.target.classList.contains('rowPick')){
    var sel = this.querySelectorAll('input.rowPick:checked').length;
    if(sel > 5){ e.target.checked = false; alert('Please select only 5'); }
  }
});

Server-side (example, decode safe JSON and use prepared statements):

$items = json_decode($_POST['payload'] ?? '[]', true);
if(is_array($items)){
  $stmt = $pdo->prepare('INSERT INTO selected (defect_code, total) VALUES (?, ?)');
  foreach($items as $r){
    if(empty($r['code'])) continue;
    $stmt->execute([ $r['code'], $r['total'] ]);
  }
}

Notes: always validate on the server (never trust client-side limits), escape output with htmlspecialchars() when echoing, and prefer PDO/mysqli prepared statements instead of deprecated mysql_*. If JavaScript might be disabled, add a graceful fallback (submit all rows or require explicit user confirmation).

Recommended Answers

All 4 Replies

Line 10, your input field has two name attributes, remove name="text" . Bye.

On line 10, it should be input type="text".

and you can also echo at the same time.

echo $check["defect_code"] . " " . $check["totalDaily"]

On line 10, it should be input type="text".

and you can also echo at the same time.

echo $check["defect_code"] . " " . $check["totalDaily"]

ok..im done do it..but i want user select only 5 value..this my coding

<script type="text/javascript">
function chkcontrol(j) {
var total=0;
for(var i=0; i < document.form1.defect_code[].length; i++){
if(document.form1.defect_code[i].checked){
total =total +1;}
if(total > 5){
alert("Please Select only five") 
document.form1.defect_code[j].checked = false ;
return false;
}
}
} </script>
</head>
<body>

<?php print(Date("d-m-Y"));?>	
<form method="POST" action="prosesMultiple.php"  name="form1">
	<?php
	include 'connection.php';
	$sql="SELECT  defect_code,dpuDaily FROM grandTotal ORDER BY dpuDaily DESC LIMIT 10 ";
	$row = mysql_query ($sql);
   	while ($check=mysql_fetch_array($row)){?>
	
	   <input type="checkbox" name="defect_code[]" id="defect_code[]" value="<?php echo $check["defect_code"];?>" onclick="chkcontrol()"; />
	   <?php echo $check["defect_code"];?> - <?php echo $check["dpuDaily"]; ?> 
	   <input   name="dpuDaily[]" id="dpuDaily[]" type="hidden"  value="<?php echo $check["dpuDaily"]; ?>"/><input name="Date[]" type="hidden" id="Date[]" value="<?php echo Date("Y-m-d"); ?>" /><br/>
   <?php } ?>
<p><input type="submit" value="Send" name="Send"></p>
</form>

the script not runing..

There's a syntax error.. you cannot place a double quote (") inside an n existing quote. For Example in Line number 27.

<input   name="dpuDaily[]" id="dpuDaily[]" type="hidden"  value="<?php echo $check["dpuDaily"]; ?>"/><input name="Date[]" type="hidden" id="Date[]" value="<?php echo Date("Y-m-d"); ?>" /><br/>

<!-- This should be like this -->

<input   name="dpuDaily[]" id="dpuDaily[]" type="hidden"  value="<?php echo $check['dpuDaily']; ?>"/><input name="Date[]" type="hidden" id="Date[]" value="<?php echo Date('Y-m-d'); ?>" /><br/>

Please Correct all this type of Errors First.

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.