ユーザーがフィールド (タイトル、説明、都市) にデータを入力し、[保存] をクリックした後に実行される次のコードを試しています。
- (IBAction)saveDataAction:(id)sender {
NSString *lEventTitle = [NSString stringWithFormat:@"var=%@",eventTitle.text];
NSString *lEventDesc = [NSString stringWithFormat:@"var=%@",eventDescription.text];
NSString *lEventCity = [NSString stringWithFormat:@"var=%@",eventCity.text];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://mydomain.com/ios/form.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
//this is hard coded based on your suggested values, obviously you'd probably need to make this more dynamic based on your application's specific data to send
NSString *postString = [[NSString alloc] initWithFormat:@"title=%@&description=%@&city=%@", lEventTitle, lEventDesc, lEventCity];
NSString *clearPost = [postString
stringByReplacingOccurrencesOfString:@"var=" withString:@""];
NSLog(@"%@", clearPost);
[request setHTTPBody:[clearPost dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:clearPost forHTTPHeaderField:@"Content-Length"];
[NSURLConnection connectionWithRequest:request delegate:self];
NSLog(@"%@", request);
}
clearPost 文字列 (サーバーに送信される変数を含む文字列) を NSLog すると、次のように表示されます。
title=xmas&description=Celebration&city=NY
PHP側では、次のように簡単です:
<?php
$title = $_POST['title'];
$description = $_POST['description'];
$city = $_POST['city'];
echo "Title: ". $title;
echo "Description: ". $description;
echo "City: ". $city;
?>
後で操作できるように、PHPでデータを取得しようとしています。今すぐ実行すると
domain.com/ios/form.php で、タイトル、説明、都市が空であることを示しています。
申し訳ありませんが、私は iOS と Objective-C に非常に慣れていません。