Jared Barrett Jared Barrett - 4 months ago 48
PHP Question

Receive echo from php (xamarin)

I'm writing an app in C# with Xamarin I have created the PHP file, if I test in the browser it works. it echoes back (ok) (error password) (error login)

I don't know how to receive it back in xamarin so I can write a basic validation

php

<?php
require_once('ConnectionInfo.php');
if(isset($_GET['Username']) && isset($_GET['Password'])){

$Username = $_GET['Username'];
$password = $_GET['Password'];

mysql_select_db($database_localhost,$con);
$query_search = "SELECT * FROM user where Username = '$Username'";
$query_exec = mysql_query($query_search) or die(mysql_error());
if($query_exec!=null){
while($result_array = mysql_fetch_assoc($query_exec)) {
$password_array = $result_array['Password'];
}
if($password == $password_array){
echo "OK";
}else{
echo "ERROR PASSWORD";
}
}else {
echo "ERROR LOGIN";
}
}else{
echo "ERROR";
}
?>


so in c# button event basically

if(ok)
{
//go to next page
} else {
//show error
}

Answer

If you just want to read the output from the php, the code below will do the trick

 WebClient client = new WebClient();

 byte[] html = client.DownloadData(http://tempuri.com/temp.php);
 UTF8Encoding utf = new UTF8Encoding();
 string res = utf.GetString(html);

   if(res == "OK")
   {
      //go to next page
   } else {
      //show error
   }