Jump to content

Recommended Posts

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

 

 

 

 

 

"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.

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 by micnie2020

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.
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.