views:

1990

answers:

2

I would like to pass multiple parameters from the iphone sdk to a server-side php which interfaces with a mySQL database.

i found some answers on how to do this, but i'm having a hard time figuring out how to include several parameters.

what i have right now is


- (IBAction)sendButtonPressed:(id)sender
{
    NSString *urlstr = [[NSString alloc] initWithFormat:@"http://server.com/file.php?date=%d", theDate];
    NSURL *url = [[NSURL alloc] initWithString:urlstr];

    [urlstr release];
    [url release];
}


which would work for 1 parameter, but what i'm looking for would be something like

http://server.com/file.php?date=value&time=value&category=value&tags=value&entry=value

how would i going about doing this?

+1  A: 

The - initWithFormat method takes multiple arguments for the format string.

So you can do things like this:

  NSString *urlstr = [[NSString alloc] initWithFormat:@"http://server.com/file.php?date=%d&second=%d&third=%d", theDate, 2, thirdIVar];

- initWithFormat works almost identically to printf() and it variants.

Here is some printf() examples http://stahlforce.com/dev/index.php?tool=csc02

Edit: Where are the variables nameField, tagsField, dreamEntry defined and set?

Unless they are NSStrings and defined in the @interface you can not uses them in this way.

I suggest hard coding some values for testing:

    NSString *urlstr = [[NSString alloc] initWithFormat:@"http://server.com/file.php?date=%@&time=%@&name=%@&category=%d&tags=%@&entry=%@", nil, nil, @"Name", nil, @"Tags", @"Dream"];
rjstelling
+1  A: 

Creating an NSURL doesn't open communications with a server. It's just a data structure for holding a URL. You want to read up on NSURLConnection.

Are all the variables you're passing in your format really numbers? %d is the placeholder for a number; %@ is an object. It's very surprising to be passing nil if you're expecting a number, even for testing purposes. It'll "work" because nil is 0, but it suggests that these aren't really numbers.

Rob Napier
considering that in this case the communication should work only one-way... the iphone doesn't get data back from the server, it just sends it, what might be missing for this to work?i switched placeholders accordingly, thanks for the heads up.
Bernardo Moreira
Creating an NSURL is like creating an NSString. It doesn't cause it to be output anywhere. You still need to connect to the server. That's what NSURLConnection does. Even though you don't want any data back, you're going to get something. At the very least you hope to get an HTTP/200, right?
Rob Napier
thanks for explaining, will refer to NSURLConnection
Bernardo Moreira