I just need help regarding my problem in editing an imploded input text from the database: I have a dynamic input text which means that you can add more input field if you click the add more button. My problem is that when tried to edit those text fields the value are showing at the input field. Example if I add 3 input fields and save it to the database and then I want to edit those 3 input fields, I should see 3 input fields but in my code it only shows one input field and inside the input field are the data that I entered in the three input fields. Hope you do understand my situation

my php code:

$children = $_POST['children'];
$cge = $_POST['age'];

$children = implode(',',$children);
$age = implode(',',$age);

$data = array(
"children"=>$children,
"age"=>$age
);
            foreach ($data as $key => $value){
            $data[$key] = sanitize_text_field($value);
            }

            $entries = "";
            foreach ($data as $row){
             $entries .= "'".$row."'";
            }

            $entries = substr($entries, 0 , -1);
            $wpdb->insert("info",$data);

the html:

 <tr>
                    <th>Names of Children</th>
                    <th>Age</th>
                </tr>
            </thead>
            <tbody>


                <tr>

                    <td><input type="text" name="children[]" class="field"></td>
                    <td><input type="number" name="cge[]" class="field"></td>

                </tr>

            </tbody>

    </table>
            </div>
<input onclick="addRoz(this.form);" type="button" value="Add" />

the javascript, for adding more children and age:

  <script type="text/javascript">
 var rozNum1 = 0;
 function addRoz(frms) {
  rozNum1 ++;
     var roz = '<tr>&nbsp<td><input type="text" name="children[]" class="field"></td><td><input type="number" name="age[]" class="field"></td></tr>';
      jQuery('#itemRoz').append(roz);
        }

my code for getting the data in the database:

$children = $_POST['children'];
$age = $_POST['age'];

$children = explode(',',$children);
$age = explode(',',$age);

<div id="itemRoz">
<table>     
            <thead>

                <tr>
                    <th>Names of Children</th>
                    <th>Age</th>
                </tr>
            </thead>
            <tbody>


                <tr>

                    <td><input type="text" name="children[]" value="<?php echo $children ; ?>"  class="field"></td>
                    <td><input type="number" name="age[]"  value="<?php echo $childrenAge; ?>"  class="
                    field"></td>

                </tr>

            </tbody>

    </table>
            </div>


    <input onclick="addRoz(this.form);" type="button" value="Add" />

I know that there is missing in my code. i just cant figure out what and I hope by posting this problem here, this will be solved. Thank you.

I saw this straight away:

$children = $_POST['children'];
$cge = $_POST['age'];
$children = implode(',',$children);
$age = implode(',',$age);

Line 4 should refer to $cge as the $age variable in the implode parameters is probably null at that point. that or rename $cge to $age.

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.