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.