EnochHaruna Posted September 26, 2015 Share Posted September 26, 2015 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 26, 2015 Share Posted September 26, 2015 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 } ?> Quote Link to comment Share on other sites More sharing options...
EnochHaruna Posted September 27, 2015 Author Share Posted September 27, 2015 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2015 Share Posted September 27, 2015 You will have to put your strings inside double quotes or heredoc (to expand variable values) or use concatenation. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.