Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-13-2009, 12:29 AM   #1 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 622
Question Posting Form with Hidden Field

I am trying to use NSURLConnection to submit a form to a URL. My form requires the inclusion of a hidden field with a specific name and value. In HTML it would look like:
Code:
 <form action="http://www.example.com/mail" enctype="multipart/form-data" method="POST">
 <input type="text" name="mailfrom">
 <input type="hidden" name="sendtoemail" value="destination-webmaster@example.com">
 <input type="submit" value="Send Email">
 </form>
I've found several threads on submitting general text fields (e.g. UITextField for entering an email address), but nothing covering how to include a hidden field that requires a specific name and value. Here's what I have so far:
Code:
NSString *post = nil;  
post = [[NSString alloc] initWithFormat:@"message=%@",messageText.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.example.com/mail"]];  
[request setHTTPMethod:@"POST"];  
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];  
[request setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];  
[request setHTTPBody:postData];  
[ NSURLConnection connectionWithRequest:request delegate:self ];  
[post release];

Thanks for any assistance.

PS-Am I going to run into trouble using "multipart/form-data" for the Content Type, instead of "application/x-www-form-urlencoded" which I see in most of the examples?
DenVog is offline   Reply With Quote
Old 09-13-2009, 06:55 AM   #2 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Just add the extra fields to the POST data:

Code:
post = [[NSString alloc] initWithFormat:@"message=%@&sendtoemail=%@",messageText.text, @"destination-webmaster@example.com"];
The fact that it's hidden in the HTML that creates the form doesn't effect the actual POST command format.

joe
FlyingDiver is offline   Reply With Quote
Old 09-13-2009, 12:27 PM   #3 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 622
Default

Quote:
Originally Posted by FlyingDiver View Post
Just add the extra fields to the POST data:

Code:
post = [[NSString alloc] initWithFormat:@"message=%@&sendtoemail=%@",messageText.text, @"destination-webmaster@example.com"];
The fact that it's hidden in the HTML that creates the form doesn't effect the actual POST command format.

joe
Hi Joe. Thanks for the response. Guess I was making it harder than it needed to be.

NOTE - Setting the form type is important. I tried using "enctype="multipart/form-data" which is what is used on the web version of my form. Does NOT work on the iPhone. Switched it back to "application/x-www-form-urlencoded" for Content-Type which I've seen in the NSURLConnection examples, and it's working fine.
DenVog is offline   Reply With Quote
Reply

Bookmarks

Tags
form, nsurlconnection, post

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 266
19 members and 247 guests
2WeeksToGo, @sandris, AdamL, ADY, Dani77, diyora, F_Bryant, GHuebner, HDshot, headkaze, mer10, Oral B, prchn4christ, Rudy, smithdale87, Thompson22, timle8n1, Touchmint, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,748
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:55 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0