php - Post a dynamic input form and fetch the array -
i have issue dynamic input form. our new adminportal (we can manage our meals , drinks there) dynamic form looks that:
...add row</button> <br /> <div class="rowfields<?= $id; ?>"> <div> <input type="text" name="newmealname[]" placeholder="name" style="width:30%"/> <input type="text" name="newmealdescription[]" placeholder="description" style="width:50%" /> <input type="number" step="0.01" name="newmealprice[]" placeholder="0.01" style="width:15%" /> <a href="#" id="remove_row">remove</a> </div> </div>
i adding more rows or remove them via jquery - works fine. passing form ajax script, using other inserts our database - works!
my php insert script 1 looks that:
<?php ... $namearray = $_post['newmealname']; $descriptionarray = $_post['newmealdescription']; $pricearray = $_post['newmealprice']; $sqladdnewmeals = $db->prepare("insert tablex(name, description, price) values(?, ?, ?)"); for($i = 0; $i<sizeof($namearray); $i++) { $sqladdnewmeals->bind_param('ssd', $_post['newmealname'][$i], $_post['newmealdescription'][$i], $_post['newmealprice'][$i]); $sqladdnewmeals->execute(); } $sqladdnewmeals->close(); ?>
i have tried use
$sqladdnewmeals->bind_param('ssdi', $namearray[$i],...
but doesn't work either. have 1 last idea, want discuss before doing that: have bind params $name, $description , use loop that:
forloop { $name=$namearray[1]; ... $sql->execute(); }
thanks in advance, pl44
edit 1: ajax pass form
$(function () { $("button#addnewmeals").click(function () { var menu_id = $(this).val(); $.ajax({ type: "post", url: "xxx/add-new-menu.php", data: $('form.managemenu-'+menu_id).serialize(), success: function () { alert("added."); }, error: function () { alert("failure"); } }); }); });
<?php $namearray = $_post['newmealname']; $descriptionarray = $_post['newmealdescription']; $pricearray = $_post['newmealprice']; $sqladdnewmeals = $db->prepare("insert tablex(name, description, price) values(?, ?, ?)"); $param = array(); for($i = 0; $i<sizeof($namearray); $i++) { $param[$i]['newmealname'] = $_post['newmealname'][$i]; $param[$i]['newmealdescription'] = $_post['newmealdescription'][$i]; $param[$i]['newmealprice'] = $_post['newmealprice'][$i]; } print_r($param); ?>
Comments
Post a Comment