简体   繁体   中英

if (isset($_POST['Name']) && isset($_POST['Number'])) always returns false in PHP

// xamarin.android c# code 
void mButtonCreateContact_Click(object sender, EventArgs e)
{
    WebClient client = new WebClient();
    Uri uri = new Uri("http://www.mydomainname.com/abcd.php");
    NameValueCollection parameters = new NameValueCollection();
    parameters.Add("Name", txtName.Text);
    parameters.Add("Number", txtNumber.Text);`
    client.UploadValuesCompleted += client_UploadValuesCompleted;
    client.UploadValuesAsync(uri, parameters);          
}

Here I am passing two parameters ater getting input from the user. However in the PHP code it always returns false in the isset['Name'] function and goes into the else statement

if (isset($_POST['Name']) && isset($_POST['Number']))
{
    $mName = $_POST['Name'];
    $mNumber = $_POST['Number'];
    echo 'true';
}
else
{
    echo 'false';
}

您需要在WebClient中设置请求参数POST。

You're currently sending a GET with the values, if you want to use POST instead, call it like this:

void mButtonCreateContact_Click(object sender, EventArgs e)
    {
        WebClient client = new WebClient();
        Uri uri = new Uri("http://www.mydomainname.com/abcd.php");
        NameValueCollection parameters = new NameValueCollection();
        parameters.Add("Name", txtName.Text);
        parameters.Add("Number", txtNumber.Text);
        client.UploadValuesCompleted += client_UploadValuesCompleted;
        client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
        client.UploadValuesAsync(uri, "POST", parameters);          
    }

用于检查POST

client.UploadValuesAsync(uri, "POST", parameters);

Best way is user $_REQUEST . WHATEVER YOU USE post or get.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM