Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jjgraz

macrumors regular
Original poster
Feb 13, 2009
103
0
I have been successful posting to a php file using the following code:

NSString *myRequestString = [[NSString alloc] initWithFormat:mad:"&first=%@", number];
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [ NSURL URLWithString: @"http://awebsite.com/file.php" ]];
[ request setHTTPMethod: @"POST" ];
[ request setHTTPBody: myRequestData ];


I have also been successful loading and parsing a TableView using the following:

NSURL *url = [[NSURL alloc] initWithString:mad:"http://awebsite.com/file.php"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

I can't figure out how to combine the two efforts though. I would like to have my TableView updated with the contents of URL, but only after the post is complete. This way the variable sent can inform the PHP file the correct data to send back.

If you have any ideas, please advise. I appreciate it.
 
I've a problem, i tried this and it doesn't work... i juste try to send my informations about my profil to a php page, but nothing appear on my php page.... do you see some errors?


NSString *myRequestString = [[NSString alloc] initWithFormat:mad:"&imei=111111&nickname=Jayz&score=999"];
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [ NSURL URLWithString: @"http://monsite.com/mapage.php" ]];
[ request setHTTPMethod: @"POST" ];
[ request setHTTPBody: myRequestData ];
 
I've a problem, i tried this and it doesn't work... i juste try to send my informations about my profil to a php page, but nothing appear on my php page.... do you see some errors?


NSString *myRequestString = [[NSString alloc] initWithFormat:mad:"&imei=111111&nickname=Jayz&score=999"];
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [ NSURL URLWithString: @"http://monsite.com/mapage.php" ]];
[ request setHTTPMethod: @"POST" ];
[ request setHTTPBody: myRequestData ];
That's because you didn't actually send anything with above codes. Try look into this line: sendSynchronousRequest:returningResponse:error:
 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:mad:"http://monsite/mapage.php"]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
[request setHTTPMethod:mad:"POST"];
[request setHTTPBody:[[NSString stringWithFormat:mad:"score=222"] dataUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if (connection) {
NSMutableData* receivedData = [[NSMutableData data] retain];
//[connection connection:connection willSendRequest:request redirectResponse:nil];
}



This code should work doesn't it ? 'cause it doesn't .... do youi know why ?
 
Erm, on it's own that won't end up with any data in receivedData. You need to read the rest of the document where you got that from, especially the connection:didReceiveData: delegate method.
 
Even if the only one things i want to do is to send some informations to my php page ? beacause it's the only one things i want to do: when a player finish game, his score is send to my php page (which make the TOP10) ...
 
Even if the only one things i want to do is to send some informations to my php page ? beacause it's the only one things i want to do: when a player finish game, his score is send to my php page (which make the TOP10) ...

Well, if I was you I'd implement all the delegate methods to allow yourself to check the responses from the server: they might actually tell you what's going on...
 
and can't we just create a method who send information to my php page, and that's all ? (create this method on a UIViewController, or delegate ...)
 
uh bboyjayz you took over the thread. :) I would really like to know what the answer to the original question was : how to post variables and receive a response in one fell swoop.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.