phpxamarinrequest

Navigation to next page : if statement doesn't work with response message in Xamarin


I have login page in Xamarin forms app, the page send request to PHP file and check the username and password, then response a message.

C# code:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using static JiyanUQuran.Models.appuser;

namespace JiyanUQuran.views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LoginPage : ContentPage
    {
        public LoginPage()
        {
            InitializeComponent();
        }
        private void Signin_Clicked(object sender, EventArgs e)
        {
            string user = username.Text;
            string pass = password.Text;

            Navigation.PushModalAsync(new MonthPage());

            string response = SendRequest("xxx/api/xxx.php?username=" + user + "&password=" + pass);
            
            if (response == "Welcome")
            {
                Navigation.PushModalAsync(new MonthPage());

            }
            else
            {
                message.Text = response;
            }
           

        }
        private string SendRequest(string url)
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    return client.DownloadString(new Uri(url));
                }
            }
            catch (WebException)
            {
                return null;
            }
        }

    }
}

the PHP page is like this :

<?php
$message = "Empty Field not Allowed";
include_once 'DbConnect.php';
$username = $_GET['username'];
$password = md5($_GET['password']);

    
    $testuser = "SELECT * FROM users Where username = '$username'";
    $testresult=mysqli_query($connection,$testuser);
    $counttest = mysqli_num_rows($testresult);

    if ($counttest == 0){
        $register=mysqli_query($connection,"INSERT INTO users Values ('','$username','$password')");
    }
    else {
        $user = "SELECT * FROM users Where username = '$username' and password = '$password'";
        $result=mysqli_query($connection,$user);
        $count = mysqli_num_rows($result);

        if ($count == 0){
            $message= "username or password is wrong";
        }
        else{
            $message ="Welcome";
        }
    }


echo $message;
?>

When the username or password wrong I received the message correctly but i don't wan to navigate to other page ,but in all response it is navigate to next page ,how can i solve this?


Solution

  • There are two Navigation.PushModalAsync methods in your Signin_Clicked function, the first one will be called directly, the second one will be called when the response equals to "Welcome".

    So your problem is caused by the first Navigation.PushModalAsync, remove it and choose whether to navigate to MonthPage by the response.

    private void Signin_Clicked(object sender, EventArgs e)
    {
        string user = username.Text;
        string pass = password.Text;
        
        //remove this line
        //Navigation.PushModalAsync(new MonthPage());
    
        string response = SendRequest("xxx/api/xxx.php?username=" + user + "&password=" + pass);
        
        if (response == "Welcome")
        {
            Navigation.PushModalAsync(new MonthPage());
    
        }
        else
        {
            message.Text = response;
        }
    }