micnie2020 Posted October 5, 2017 Share Posted October 5, 2017 function save(){ var Name= $("#Name").val(); var Date = $("#Date").val(); var t = $('#example').DataTable(); var data = t.rows().data(); if(data.length != 0) { var tblMaster ={ "Name ":"", "Date":"", "tblSTPDet":[] }; var tblDet = { "Item":"", "Qty":"", "Price":"" }; tblMaster.Name = Name; tblMaster.Date = Date; for(var i=0; i<data.length; i++) { tblDet.Item = data[i][0]; tblDet.Qty = data[i][1]; tblDet.Price = data[i][2]; } tblMaster.tblDet.push(tblDet); var tblDet = { "Item":"", "Qty":"", "Price":"" }; $.ajax({ url: 'save.php', data: JSON.stringify(tblMaster), type: 'POST', contenttype: 'application/json', async: false, error: function(request){ alert(request.responseText); }, success: function(result){ if(result==true) { alert("Successfully Record Saved!"); location.href="INDEX.php"; }else{ alert("Failed"); return false; } } }); } else{ alert("No Details Data available!"); return false; } } [HttpPost] public JsonResult Editing(MMainTbl MainData) { tblRequisition tblRequisitions = new tblRequisition(); tblRequisitions.Name = MainData.Name; tblRequisitions.Date = MainData.Date; success = _repository.AddREQ(tblRequisitions); foreach (SSubTbl ss in MainData.tblSubItem) { tblReqItem tblReqItems = new tblReqItem(); tblReqItems.Item = ss.Item; tblReqItems.price = ss.price; tblReqItems.qty = ss.qty; success = _repository.AddItem(tblReqItems); } } Hi All, How to write save.php for ajax post data as above. I only new how to write in C# code as above. Please help, I need save.php. Thank you. Regards, Micheale Quote Link to comment https://forums.phpfreaks.com/topic/305215-savephp-for-ajax-post-2-dimension-data-master-details/ Share on other sites More sharing options...
requinix Posted October 5, 2017 Share Posted October 5, 2017 "Name ":"",Remove the extra space, data: JSON.stringify(tblMaster),remove the JSON.stringify part, async: false,remove all of that, then make save.php contain <?php http_response_code(400); var_export($_POST);and try to save a small table of data. You should get a popup that tells you what's in $_POST. Use that with knowledge of how to process HTML forms that you may or may not have with however your database access works. Quote Link to comment https://forums.phpfreaks.com/topic/305215-savephp-for-ajax-post-2-dimension-data-master-details/#findComment-1552406 Share on other sites More sharing options...
micnie2020 Posted October 7, 2017 Author Share Posted October 7, 2017 (edited) Hi All, I try this but no luck, got error on this line for(var i=0; i<$detail.length; i++){ ####<br/> <b>Parse error</b>: syntax error, unexpected T_VAR, expecting ';' in ... function save(){ var Name= $("#Name").val(); var Date = $("#Date").val(); var t = $('#example').DataTable(); var data = t.rows().data(); if(data.length != 0) { var tblMaster ={ "Name":"", "Date":"", "tblSTPDet":[] }; var tblDet = { "Item":"", "Qty":"", "Price":"" }; tblMaster.Name = Name; tblMaster.Date = Date; for(var i=0; i<data.length; i++) { tblDet.Item = data[i][0]; tblDet.Qty = data[i][1]; tblDet.Price = data[i][2]; } tblMaster.tblDet.push(tblDet); var tblDet = { "Item":"", "Qty":"", "Price":"" }; $.ajax({ url: 'save.php', data: tblMaster, type: 'POST', contenttype: 'application/json', error: function(request){ alert(request.responseText); }, success: function(result){ if(result==true) { alert("Successfully Record Saved!"); location.href="INDEX.php"; }else{ alert("Failed"); return false; } } }); } else{ alert("No Details Data available!"); return false; } } save.php <?php $Name = $_POST['Name']; $Date = $_POST['Date']; $detail = $_POST['tblDet']; session_start(); require_once 'class.user.php'; $user_home = new USER(); $rs = $user_home->runQuery("insert into tblMaster(Name)values(:Name,:Date)"); $rs->bindParam(':Name', $Name); $rs->bindParam(':Date', $Date); $result2 = $rs->execute(); if ($result2){ $ids = $user_home->lasdID(); echo $detail.length; for(var i=0; i<$detail.length; i++){ ####<br/> <b>Parse error</b>: syntax error, unexpected T_VAR, expecting ';' in ... $stmt = $user_home->runQuery("insert into tblDet(master_id,Item,Qty,Price)values(:master_id,:Item,:Qty,:Price)"); $stmt->bindParam(':master_id', $ids, PDO::PARAM_INT); $stmt->bindParam(':Item', $detail[i].Item); $stmt->bindParam(':Qty', $detail[i].Qty); $stmt->bindParam(':Price', $detail[i].Price); $result = $stmt->execute(); } echo json_encode(array( 'id' => $ids )); } else{ echo json_encode(array('errorMsg'=>'Some errors occured.')); } ?> Please advise. Thank you. Regards, Micheale Edited October 7, 2017 by micnie2020 Quote Link to comment https://forums.phpfreaks.com/topic/305215-savephp-for-ajax-post-2-dimension-data-master-details/#findComment-1552476 Share on other sites More sharing options...
requinix Posted October 7, 2017 Share Posted October 7, 2017 Seems you've forgotten some important parts of the PHP language works. echo $detail.length; for(var i=0; i$stmt->bindParam(':Item', $detail[i].Item);Wrong syntax. Try again. Quote Link to comment https://forums.phpfreaks.com/topic/305215-savephp-for-ajax-post-2-dimension-data-master-details/#findComment-1552477 Share on other sites More sharing options...
micnie2020 Posted October 7, 2017 Author Share Posted October 7, 2017 (edited) Hi, Sorry. I am new to php. Can you show me the sample skeleton code please? Is this for($i=0; $i<$detail.length; $i++){ ? Thank you. Regards, Micheale Edited October 7, 2017 by micnie2020 Quote Link to comment https://forums.phpfreaks.com/topic/305215-savephp-for-ajax-post-2-dimension-data-master-details/#findComment-1552478 Share on other sites More sharing options...
requinix Posted October 7, 2017 Share Posted October 7, 2017 echo $detail.length;1. PHP uses "." for string concatenation. Use -> for object member access. for(var i=0; i2. PHP does not use "var" for local scope variables. 3. Variables start with a $. 4. PHP arrays do not have a .length member. Use count(). $stmt->bindParam(':Item', $detail[i].Item);Same problems. Also, $detail = $_POST['tblDet'];that won't work properly. Look at what you're POSTing. Quote Link to comment https://forums.phpfreaks.com/topic/305215-savephp-for-ajax-post-2-dimension-data-master-details/#findComment-1552480 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.