2011-01-29 111 views
4

我創建通過以下POST方法客觀-CPOST方法

-(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]; 
     } 

和我的PHP代碼:

if (isset($_POST['submit']){ 

    //it doesn't get into this if statement 
    $latitude = $_POST['latitude']; 
    $longitude = $_POST['longitude']; 

} 

的問題是,爲什麼不能PHP代碼進入if語句?我在哪裏可以通過Objective-C來陳述'提交'?

+0

你有沒有找到答案?我有這個確切的問題:http://stackoverflow.com/questions/13110180/php-get-value-erased-when-placed-in-ifisset – Celeste 2012-10-29 10:08:43

回答

3

您的查詢字符串POST不包含submit密鑰。你應該將其更改爲以下

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

顯然它仍然沒有任何東西....我怎麼能檢查我的PHP代碼,它實際上觸發了if語句中的代碼? – aherlambang 2011-01-29 05:01:59

0

也許,這是簡單的使用if($_SERVER['REQUEST_METHOD'] == 'POST'),而不是你現在把檢查形式後的代碼。

2

參數之間沒有「&」。

2
-(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"]; 
}