我正在尝试在用户在字段(标题、描述、城市)中输入数据然后单击保存后执行的以下代码。
- (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);
}
当我 NSLog clearPost 字符串(包含要发送到服务器的变量的字符串)时,它显示:
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 很陌生。