Jump to content

Submit multiple table rows with input elements to database


EnochHaruna

Recommended Posts

Hi, am a new user here and also a beginner. I've been working on a project and got stuck. I have a table which I dynamically(using ajax) add rows that comprises of input text, select box and text area using the following code

 

$("#add_row").on("click", function()

              {

            var pam = $("tr:last-child td:first-child").html();

             if(pam=="s/n") pas=1;

             else

                {

                     var past = parseInt(pam);

                      var pas = (past) + 1;

                 }

           $.ajax(

                     {

                       url:"../Admin_Manager/financeHandler.php?opt=income_tbl&adrw="+pas,

                       type:"POST", dataType:"json",

                        error: function()

                        {
                            alert("error now");

                         },

                        success: function(data)

                       {

                            $("#table_inc").append(data.table);

            

                            $('#numrows').html(data.numrows);

                      }

 

 

               });

 

 

});

 

 

 

<?php

 

require_once "./database/Model.php";

$db=new Model();

if(isset($_GET['opt']))

{

  $opt = trim($_GET['opt']);

  switch ($opt)

  {

   case 'income_tbl':

   $pam = $_GET['adrw'];

 

   $table='<tr id="row'.$pam.'"><td>'.$pam.'</td><td><select id="item_name'.$pam.'" name="i_name'.$pam.'">

                                                   <option value="tithe">Tithes</option>

                                                   <option value="offer">Offering</option>

                                                   <option value="donations">Donations</option>

                                                   <option value="others">Others</option>

                                                  </select></td><td>

            <textarea type="text" rows="3" cols="30" name="inc_desc'.$pam.'"  id="inc_desc'.$pam.'"></textarea>

            </td><td><form class="form-inline"><div class="form-group">

                     <label class="sr-only" for="in_Amount">Amount (in Naira)</label>

                     <div class="input-group"><div class="input-group-addon">₦</div>

                     <input type="text" class="form-control" name="inc_amt.'.$pam.'" id="inc_amount'.$pam.'" placeholder="Amount">

                     <div class="input-group-addon">.00</div></div></div></form></td><td>

                     <input type="date" name="inc_date'.$pam.'" id="date_in'.$pam.'"></td></tr>';

   echo json_encode(array("table" => $table, "numrows" => $pam ));

   break;

?>

 

 

Am confused about how to submit multiple rows of the table to my database. Thanks in advance.

Link to comment
Share on other sites

Instead of using names with suffixes like

name="inc_amt".$pam

use names like

name="inc_amt[$pam]"

That way your inputs are posted in arrays which can easily loop through.

<?php
     foreach ($_POST['item_name'] as $pam => $name) {
         $inc_desc = $_POST['inc_desc'][$pam];
         $inc_amt = $_POST['inc_amt'][$pam];
         
         // process $pam, $name, $inc_desc, $inc_amt
     }
?>
Link to comment
Share on other sites

 

Instead of using names with suffixes like

name="inc_amt".$pam

use names like

name="inc_amt[$pam]"

That way your inputs are posted in arrays which can easily loop through.

<?php
     foreach ($_POST['item_name'] as $pam => $name) {
         $inc_desc = $_POST['inc_desc'][$pam];
         $inc_amt = $_POST['inc_amt'][$pam];
         
         // process $pam, $name, $inc_desc, $inc_amt
     }
?>

Thanks for your help. After effecting your suggestion, I wrote a validation to check the value of the selected item_name but it was empty. Same goes for the other inputs. upon inspecting elements, i got

<select id="item_name1" name="i_name[$pam]">
		<option>...select...</option>
		<option value="tithe">Tithes</option>
		 <option value="offer">Offering</option>
	        <option value="donations">Donations</option>
		<option value="others">Others</option>
	 </select>

what am i doing wrong? thanks for your patience.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.