here i am having a form like this .i am having multiple checkboxes that are coming from database and i have given a field with every checkbox...now suppose 10 checkboxes are coming from database..now i want to insert values in database only of those checkboxes which are checked along with their textbox values..here is my php script.when i click on submit button the checkbox values are getting inserted right but the textbox values are not getting inserted... can anyone help me with the code ??

  $query2="select prodcode,prodname from product";
  echo"<table border='2' align='center' cellpadding='0' cellspacing='0' style='width:50%'>";
  echo"<td>"."<input type='checkbox' id='checkbox1[]' value='".$row['prodcode']."' name='checkbox1[]'/>".$row['prodcode']."</td>";
  echo"<td>"."<input type='textbox' maxlength='4' id='quan' name='quantity[]'  class ='quantity' />"."</td>";
 ?> <tr> <td colspan="4" align="center"><input class="submit" type="submit" 
 name="btnsubmit" value="Submit"  onclick=" return validate();"/></td></tr> <?php
for ($i=0; $i<sizeof($check);$i++)
$query="INSERT INTO elevconfigdetails (id,prodcode,quantity)
        VALUES ('$id','".$check[$i]."','".$quantity[$i]."' )";
        mysqli_query($con,$query) or die ('Error updating database'.mysqli_error($con));
       echo "</br>Record is inserted.";
1 Year
Discussion Span
Last Post by AleMonteiro

Checkboxes values are only submited if they're checked, and text values are always submited even if blank.
So if you check only 5 boxes, the length of $_POST['checkbox1'] will be 5 but the length of $_POST['quantity'] will still be 10.

I suggest you to name each input text with the correspoding prodcode used on the checkbox, like name='quantity_".$row['prodcode']."' and then for each checked input you get it's text using $_POST["quantity_".$check[$i]].

Edited by AleMonteiro


@AleMonteiro thanks for reply
but the problem is if i check 3 checkbox and enter values into 3 textbox field only 1 textbox value i get not 2 and 3 textbox values.i just echo that $POST["quantity".$check[$i]].but i will give me only first textbox values
so please help...


ohhh srry @AleMonterio .finally my problem is solved ..the checkbox and the corresopding textbox values get inserted into database .inside of printing that $POST["quantity".$check[$i]].i directly insert it into INSERT QUERY..so both checkbox and textbox value is get inserted into database. thank u so much for your help.i am trying to solve this problem from 1 week...finally you help me lotss
thanks for your help.


@AleMonterio are you here..?throught your help and suggestions my problem is solved .but another problem i am facing is that the textbox validation...i want user must be enter value in atleast one textbox..
throught 'name' attribute of textbox field i am giving the validation for textbox..but the problem is that i need to insert values in each textbox but again it will give me alert msg that "plzzz insert value in at least one textbox"
plzzzz help me ..

<script language="javascript">
function validation()

 var chks = document.getElementsByName('quantity');//here quantity[] is the name of the                 textbox 
 //var chks=document.getElementById('quan[]').value;
        var flag=0;                     
        for (var i = 0; i < chks.length; i++) 
            if (chks[i].value!="") 

        if (flag==0)
                alert("Please Fillup Atleast One Textbox");
                return false;
       // else return true;

/////throught submit button calling the validation function//////////
<td colspan="4" align="center"><input class="submit" type="submit" 
     name="btnsubmit" value="Submit"  onclick=" return validation();"/>
///////And The Textbox field is present in above problem////     

But you're not checking for any text, you're just checking the checkbox value that will always be the same.

I'd go with something like this:

var checkedCount = 0,
    errMessage = undefined;

for (var i = 0; i < chks.length; i++) 
    var chk = chks[i],
        // gets the checkbox value
        chkValue = chk.value, 
        // gets the text input related to that checkbox
        txt = document.getElementsByName('quantity_' + chkValue)[0],
        // gets the text input value
        txtValue = txt.value;

    if ( chk.checked === true || chk.checked === "checked" ) {
        checkedCount = checkedCount + 1;
        // Check if there's value on the text and if the text is not only blank spaces
        if (typeof txtValue !== 'string' || txtValue.toString().replace(/ /gi, "").length === 0 ) 
            errMessage = "You must fill all selected inputs";

if ( checkedCount === 0 ) errMessage = "You must select at least one input";

if ( errMessage === undefined ) {
    return true;
return false;

Obs.: Not tested.

And I think you shouldn't have re-open this thread. Since is another problem, in another language, you should have started a new thread.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.