/*
`I have some dynamic checkboxes and corresponding textboxes.

When a user ticks a checkbox and enters data into the corresponding textbox and submits, the value of the checkbox and textbox should be inserted into the database. I can insert the checkbox values perfectly, but the textbox values have issues.

How I can do it ?

Below is the code.. I am not able to print checkbox's textbox value`
*/

if(isset($_POST['submit']))
    {

        $email=array();  // Checkbox array
        $different_kvs=array(); // textbox array
        foreach($_POST['contactid'] as $i => $contenti)
                $email[$i]=$contenti;

        $elements = count($_POST['kv']);
        if($elements > 0) 
        {
            for($i = 0; $i <= $elements; $i++) 
            {
                echo $_POST['kv'][$i];
                echo $email[$i]; // Line number 26
            }
        }
    }

Change line 12 to:

for ($i = 0; $i < $elements; $i++)

You are processing one element too many.

I changed it already but same error is still showing..

Then the length of those two arrays differ. Output the content of both and check what's wrong.

I tried all the things.. like only two checkboxes are selected and entering values into there corresponding textboxes still showing Notice: Undefined offset: i.e. echo $email[$j];

Complete code:

<?php

if(isset($_POST['submit']))
{
//echo print_r($_POST['contactid']);
    if(isset($_POST['equalkv']))
        $kv=$_POST['equalkv']; 
    if(isset($_POST['equallp']))
        $lp=$_POST['equallp'];

    $email=array();     
    $different_kvs=array();
    foreach($_POST['contactid'] as $i => $contenti)
            $email[$i]=$contenti;

    $elements = count($_POST['kv']);
    if($elements > 0) 
    {
        for($j = 0; $j <= $elements; $j++) 
        {
            echo $_POST['kv'][$j];
            echo $email[$j];
        }
    }
}
?>

<form method="post" id="form_id" name="myform">

<div class="alert alert-success">
<table style="font: normal 11px Verdana; width:100%">
    <tr>
        <td width="172">
        <a href="#" onClick="select_all('contactid', '1');">Check All</a> | 
        <a href="#" onClick="select_all('contactid', '0');">Uncheck All</a>       </td>
        <td width="100">Transfer Equal to all</td>
        <td width="50"><input type="text" name="equalkv" style="width:50px;"></td>
        <td width="29"> KVs</td>
        <td width="50"><input type="text" name="equallp" style="width:50px;"></td>
        <td width="29"> LPs</td>

    </tr>
</table>
</div>
<table style="border:1px solid #d6e9c6; width:100%; border-top:none; border-right:none; border-bottom:none;  " cellpadding="0" cellspacing="0">
    <tr>
        <td>
        <table style=" width:100%" class="" cellpadding="0" cellspacing="0">
        <tr>
        <?php 
        $result=mysql_query("select * from contact where login_status=1");
        $i=1;
        while($row=mysql_fetch_array($result)) 
        { 
        ?>
        <div>
        <td style="vertical-align:top; padding:20px 0 20px 10px; border-bottom: #d6e9c6 1px solid;  " colspan="2"  >
        <div style="height:60px;">
        <input type="checkbox" value="<?php echo $row['emailAddress']; ?>" name="contactid[]" style="float:left;">
        <img src="images/yellow-camera-icon.png" style="margin:0 10px 0 10px;">
        </div>
        <div style="vertical-align:bottom; ">
        <table>
            <tr>
                <td><input type="text" name="kv[]" style="vertical-align:bottom; width:50px;"></td>
                <td style="font:normal 11px Verdana;">KV</td>
            </tr>
        </table>
        </div>
        </td>

        <td colspan="2" style="vertical-align:top;  padding:20px 0 20px 0; border-right: #d6e9c6 1px solid; border-bottom: #d6e9c6 1px solid; ">
            <div style="height:60px;">
                <div style="height:15px; font:bold 11px Verdana">
                <?php
                echo strtoupper ($row['firstName']).' ';
                if(strlen($row['midleName'])) echo strtoupper ($row['midleName']).' ';
                if(strlen($row['lastName'])) echo strtoupper ($row['lastName']).' ';
                ?>
                </div>
                <div style="height:15px; font:normal 11px Verdana">
                <?php
                if(strlen($row['company_name'])==0)
                    echo "-"; 
                else
                    echo $row['company_name'];
                ?>
                </div>
                <div style="height:15px; font:normal 11px Verdana">
                <?php
                if(strlen($row['designation'])==0)
                    echo "-"; 
                else
                    echo $row['designation'];
                ?>
                </div>
            </div>
            <div style="vertical-align:bottom; font:normal 11px Verdana;">
        <table>
            <tr>
                <td><input type="text" name="lp[]" style="vertical-align:bottom; width:50px;"></td>
                <td style="font:normal 11px Verdana;">LP</td>
            </tr>
        </table>
        </div>

        </td>
        </div>
        <?php
        if($i%2==0)
            echo "</tr><tr style='padding-bottom:20px;' >";   
        $i++;           
        }   
        ?>  
        </tr>
        </table>
        </td>
    </tr>
</table>
<div class="alert alert-success">
<div style="font:bold 13px Verdana; color:#FFFFFF; text-align:center">
<input type="submit" name="submit" value="Transfer" />
</div>
</div>
</form>

The simple fix would be to use:

if (isset($email[$i]))
    echo $email[$i];

But you need to find the cause, not fix the effect.

Mine problem in brief: I tried all the things to do but not able to figure out.
I have some dynamic checkboxes and corresponding textboxes.
When a user ticks a checkbox and enters data into the corresponding textbox and submits, the value of the checkbox and textbox should be inserted into the database. I can insert the checkbox values perfectly, but the textbox values have issues. How I can do it ?
I am not able to print checkbox's textbox value. I already posted entire code.

Just before your if ($elements > 0) add the following code, and show what it outputs here:

print_r($_POST['kv']);
print_r($email);

Array ( [0] => 100 [1] => [2] => [3] => [4] => [5] => [6] => [7] => 100 [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => ) Array ( [0] => haroonans123@gmail.com [1] => rahulkaw_79@yahoo.co.in ) 100haroonans123@gmail.comrahulkaw_79@yahoo.co.in

with 17 error messages : i.e.
Undefined offset: ---- on line 26 // line no. 26 is echo $email[$j];

Your loop loops 17 times, while the email array only holds 3 items. That's the problem.

Am I correct in saying the array's should only contain the information of the textboxes having checked checkboxes? And why does the first array contain only empty values?

Then, how I can solve this problem..? synchronising both checkbox and textbox loops..?

Do you have this online somewhere so I can view this? If not, post a screenshot explaining what's what.

I'm attaching the screenshot; and want to process only yellow boxes; as an example; it will vary..

The problem is that an unchecked checkbox does not get posted. You need a way to match the checkbox with the textbox, which can be done by using the index as a value (instead of the email), starting at 0. If you need the email you can create a hidden input array for them, similar to the textboxes.

Maybe try this (line 26):
echo isset($email[$j])?$email[$j]:"";

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.