POST method in objective-C and PHP

I created the following POST method via objective-C

-(IBAction) postLocation: (id) sender{ NSString *latitude = @"37.3229978"; NSString *longitude = @"-122.0321823"; NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.mydomain.me/webservice.php"]]; [request setHTTPMethod:@"POST"]; NSString *post =[[NSString alloc] initWithFormat:@"latitude=%@longitude=%@submit",latitude,longitude]; [request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]]; NSURLResponse *response; NSError *err; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; } 

and my php code:

 if (isset($_POST['submit']){ //it doesn't get into this if statement $latitude = $_POST['latitude']; $longitude = $_POST['longitude']; } 

The question is why PHP code cannot get into the if statement? Where can I specify 'submit' via objective-C?

+4
source share
4 answers

The request line for POST does not contain a submit key. You should change it to the following

 NSString *post =[[NSString alloc] initWithFormat:@"latitude=%@&longitude=%@&submit=",latitude,longitude]; 
+3
source

No "&" s between parameters.

+2
source
 -(IBAction) postLocation: (id) sender{ NSString *latitude = @"37.3229978"; NSString *longitude = @"-122.0321823"; NSString *post = [NSString stringWithFormat:@"&latitude=%@&longitude=%@", latitude, longitude]; NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; NSString *address_url = @"http://www.mydomain.me/webservice.php?"; address_url = [address stringByAppendingString:post]; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:address]]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"]; } 
+2
source

It is probably easier to use if($_SERVER['REQUEST_METHOD'] == 'POST') instead of the code you have now to check the form mail.

0
source

Source: https://habr.com/ru/post/1337565/


All Articles