Q:

Sending label.text to a php file

i am trying to send a label.text to a variable name "vin" in a php file on server. here is my code please help to debug if anything wrong in it. thanks
NSString *post =[[NSString alloc] initWithFormat:@"vin: %@",codeLabel.text];
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];

    NSURL *url = [NSURL URLWithString: @"http://url here/test.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    [NSURLConnection sendAsynchronousRequest:request queue:nil completionHandler:nil];

    NSURLResponse *response;
    NSError *error;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if(error==nil)
        NSLog(@"Error is nil");
    else
        NSLog(@"Error is not nil");
    NSLog(@"success!");

it always print "Error is not nil" :(

A:

Try it....
Set Delegate:

<NSURLConnectionDataDelegate,NSURLConnectionDelegate>

Declare :

NSMutableData *responceData;
NSURLConnection *connect;

Code:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

responceData = [[NSMutableData alloc]init];

 NSString *post = [NSString stringWithFormat:@"op=post_comment&business_id=%@&name=%@&email=%@&comment=%@",busId, name.text,email.text,myTextView.text];
 NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

 NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

 NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
 [request setURL:[NSURL URLWithString:@"http://www.dignizant.com/red_pages/web_services/index.php"]];
 [request setHTTPMethod:@"POST"];
 [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
 [request setHTTPBody:postData];
 connect = [NSURLConnection connectionWithRequest:request delegate:self];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[responceData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR : %@" , error);

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:[NSString stringWithFormat:@"%@",error] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//initialize convert the received data to string with UTF8 encoding
NSString *responceString = [[NSString alloc] initWithData:responceData
                                                 encoding:NSUTF8StringEncoding];
NSLog(@"%@" , responceString);

NSMutableDictionary *message = [responceString JSONValue];
[message valueForKey:@"message"];

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:[message valueForKey:@"message"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}

Hope i helped

