I am new to React-Native and PHP, I am trying to send an array from react-native to PHP on my server with code like this :
SaveData() {
let { RoomNo } = this.state
let q = [{"0":"BVR001","Stock_Code":"BVR001","1":0,"Qty":0},
{"0":"BVR015","Stock_Code":"BVR015","1":0,"Qty":0},
{"0":"BVR017","Stock_Code":"BVR017","1":0,"Qty":0}]
let url = 'http://192.168.100.99/SaveMNB.php'
fetch(url, {
method : 'POST',
headers : {
'Accept' : 'application/json',
'Content-Type' : 'application/json'
},
body : JSON.stringify({
RoomNo : RoomNo,
Orderan : q
})
})
.then(res => res.json())
.catch(err => Alert.alert('ERROR',err.toString(),
[{text: 'OK'}],{ cancelable: false }))
}
while on the PHP side my code is as below :
<?php
include("dbconfig.php");
$json = file_get_contents("php://input");
$obj = json_decode($json,true);
$RoomNo = $obj['RoomNo'];
$OrderanX[] = $obj['Orderan'];
$con = sqlsrv_connect($myServer,$MyDbR);
if ( $con === false ) {
die( print_r( sqlsrv_errors(), true));
}
foreach($OrderanX As $Order) {
//HERE WILL BE LOOPING THE ARRAY AND PUT IT INTO DATABASE
}
echo json_encode('SUCCESS');
?>
This will always return error because i am unable to pass the array from react-native to php.
The Error is
SyntaxError: JSON Parse error: Unrecognized token '<'
How to pass the array from react-native to php, thanks, helps are greatly appreciate.
EDIT :
However if i delete the foreach loop and change code to
echo json_encode($OrderanX);
it will return
[object Object],[object Object].[object Object]