1

I want to insert data from my Form to my server using HTTP Web POST. I have my code below I am unable to get the value of JObject json and send it into my php code.

var caf = entCafNo.Text;
string url = "http://192.168.120.9:7777/TBS/mobile-request.php?Host=" + Constants.hostname + "&Database=" + Constants.database + "&Request=SendCaf";
string contentType = "application/json";
JObject json = new JObject
{
  { "CAF", caf }
};

HttpClient client = new HttpClient();
var response = await client.PostAsync(url, new StringContent(json.ToString(), Encoding.UTF8, contentType));

PHP Code:

$request = $_GET["Request"];

if($request == "SendCaf"){
    $caf = $_POST["CAF"];

    $sql = "INSERT INTO tblCaf(CAFNo) 
            VALUES('$caf)";
    mysqli_query ($conn, $sql);
}
loot verge
  • 449
  • 1
  • 12
  • 31
  • 1
    Your script is wide open to [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's – RiggsFolly Aug 18 '18 at 14:16
  • Try adding a `print_r($_POST); print_r($_GET);` to see what is being sent to the PHP – RiggsFolly Aug 18 '18 at 14:17
  • @RiggsFolly There is no data being get by my php code – loot verge Aug 18 '18 at 14:30
  • @RiggsFolly I fixed my link I forgot the php file in the link how can i get the data `{ "CAF", caf }, { "CAF2", caf2 }` – loot verge Aug 18 '18 at 14:42
  • With that error, I would guess (dont know xamarin) that the URL is wrong – RiggsFolly Aug 18 '18 at 14:44
  • @RiggsFolly its ok now I fix my link the problem now is to get the `{ "CAF", caf }` and insert it into my database – loot verge Aug 18 '18 at 14:45
  • @RiggsFolly print_r($_POST); value is 1 not the data I needed – loot verge Aug 18 '18 at 14:57

1 Answers1

1

Use this code:

 $json_str = file_get_contents('php://input');
    $json_obj = json_decode($json_str);
    $caf = $json_obj->CAF;

    $sql = "INSERT INTO tblCaf(CAFNo) 
            VALUES('$caf')";
    mysqli_query ($conn, $sql);
loot verge
  • 449
  • 1
  • 12
  • 31