0

I have searched and searched to find an answer, however none of the logic that should work is working. If anyone could please point my in the right direction that would be greatly appreciated.

First, I am trying to get the text values from a textfield in iOS to my PHP file so I can run them in a MySQL query, and then return information back to the application. Before I go through all of that, I am trying to simply send a POST request and display in my php file.

Here is the iOS code:

-(IBAction)post:(id)sender{
    NSString *post = [NSString stringWithFormat:@"Username=%@&Password=%@",@"username",@"password"];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://www.salamanderdevelopment.com/login.php"]];
    [request setHTTPMethod: @"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if(conn) {
        NSLog(@"Connection Successful");
    } else {
        NSLog(@"Connection could not be made");
    }
}

If I am correct, this should send data with parameter name Username and value username as well as parameter name Password and value password to the send php by a Post method. The code is initiated by the click of a button.

Now here is the PHP that I believe should echo the associated array of this post method that is sent.

<?php 

 var_dump($_POST);

?>

The URL is correct and the PHP file name is correct. I've run this on iOS simulator and on device. All I get is a log of "Connection Successful" in both circumstances. When I try to go to the PHP file name online I get "array(0){}"

If I could successfully pass one post request then I believe I can manage the rest of the big picture.

Here is one of the many links I have tried to implement and unfortunately could not successfully do so.

Sending an HTTP POST request on iOS

EDIT: Using file_put_contents that Aziz Saleh suggested below, I was able to see my progress in "real time" My problem is now solved. Thank you.

Community
  • 1
  • 1
  • Things like this (where I can't access it via the browser directly) I would do a `file_put_contents('/location/of/file', print_r($_POST, true));` and read the file after I access it from the iOS. This will give you the exact data the user would be getting. – Aziz Saleh Dec 13 '15 at 22:14
  • Thank you. This works to finally show the POST array that I am sending. I haven't gotten this far so let me see what I can do with this and hopefully my questions is answered. – Kevin A. Bartchlett Dec 13 '15 at 23:27
  • Simply, by using the file_put_contents suggestion I was able to find my errors, and now successfully get the large picture done. I thank you so much. You have been a huge help to a massive stress of mine. Why would the var_dump($_POST) not show the output just as if the file_put_contents would? Again thank you. – Kevin A. Bartchlett Dec 13 '15 at 23:46
  • Because `file_put_contents` records the information the instant the call is made (there is data) opposed to when you access the page via GET (direct url access) POST is empty. It is like telling it store the info once it happens and I will check it at my convenience. – Aziz Saleh Dec 14 '15 at 00:06

0 Answers0