I swear I had this working but now it doesn't. Seems totally straight
forward but I have no idea why it is broken and I am about to scream.
I made a simple web form:
When you submit the form, it goes to form2.php and displays the
"name" on the screen. Simple. It works in the browser.
I am running this code in Xcode and it keeps NSLogging the code from
the first page, but it should be submitting the form and showing me
the output from the second page. I know I had this working before so
I must be missing something so basic. Please help before I totally
freakout.
forward but I have no idea why it is broken and I am about to scream.
I made a simple web form:
Code:
<form method="POST" action="form2.php">
<input type="text" name="name">
<input type="submit">
</form>
When you submit the form, it goes to form2.php and displays the
"name" on the screen. Simple. It works in the browser.
I am running this code in Xcode and it keeps NSLogging the code from
the first page, but it should be submitting the form and showing me
the output from the second page. I know I had this working before so
I must be missing something so basic. Please help before I totally
freakout.
Code:
NSString *post = @"name=Bob";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://www.url.com/myform.php"]]; // changed the url
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"Succeeded! Received %d bytes of data",[urlData length]);
NSString *outputdata = [[NSString alloc] initWithData:urlData
encoding:NSASCIIStringEncoding];
NSLog(outputdata);