I have two file lets assume 1.php and 2.php

1.php is my fetch data so it contains

echo '<tr onclick="javascript:showRow(this);">';
echo "<td><input type=\"checkbox\"  name=\"user_id[]\" value='".$user_id."'/>$user_id</td>";

my second php 2.php contains

<script type="text/javascript">
function showRow(row)
{
    var x=row.cells;
    document.getElementById("custID").value = x[3].innerHTML;
}
</script>

and

 <input class="form-control" name="event_name" id="custID" type="text" maxlength="255" />

My problem is that it only displays 1 value from the checkbox to the textbox. I would like it to display multiple value from the multiple checkbox so i could send multiple value to a form post. Thanks in advance fellow stranger

1 Answers

2
Serene Abraham Mathew On Best Solutions

may be this is what you want . just use addValue instead of your showRow function

function addValue(input){
  //select all checkboxes with name userid that are checked
  var checkboxes = document.querySelectorAll("input[name='user_id[]']:checked")
  
  var values = "";
  
  //append values of each checkbox into a variable (seperated by commas)
  for(var i=0;i<checkboxes.length;i++){
    values += checkboxes[i]
.value + ","  }

//remove last comma
  values = values.slice(0,values.length-1)
  
  
  //set the value of input box
  document.getElementById("custID").value = values;
  
}
<table>
<!-- Change the onclick event handler from tr to checkbox -->
<tr >
  <td><input type="checkbox" onclick="addValue(this)"  name="user_id[]" value='1'/>1</td>
</tr>
<tr >
  <td><input type="checkbox" onclick="addValue(this)" name="user_id[]" value='2'/>2</td>
</tr>
<tr >
  <td><input type="checkbox" onclick="addValue(this)" name="user_id[]" value='3'/>3</td>
</tr>
</table>

<input class="form-control" name="event_name" id="custID" type="text" maxlength="255" />