1

Something i wanna know is ...When i send empty parameters to php using post method, it sending as (null). let me elaborate. this is php post method in my ios

NSString *urlString = [NSString stringWithFormat:@"http://localhost:8888/userinfo.php?name=%@&Email=%@&phone=%@",name,Email,phone];
 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

In my app, user should not fill all parameters, user should fill only one. so,if user enters only phone and press send,remaining fields are getting stored as (null) in database instead of emptiness.

this is how my app executing php file, when null parameters sent

http://localhost:8888/userinfo.php?name=(null)&Email=(null)&phone=123445435"

what i want is

http://localhost:8888/userinfo.php?name=&Email=&phone=123445435"

is this possible?

1

3 Answers 3

3

try this :

NSString *urlString = [NSString stringWithFormat:@"http://localhost:8888/userinfo.php?name=%@&Email=%@&phone=%@",
 (name?name:@""),
 (Email?Email:@""),
 (phone?phone:@"")];
 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator. condition ? first_expression : second_expression;

Sign up to request clarification or add additional context in comments.

4 Comments

simple and awesome.!thanks it worked. so it works even if two parameters are not null and one parameter is null? or however? @tdelepine. i got code but i didn't understand code? can u tell what does it ((name?name:@""),) mean?
By the way, you can use email ?: @"" much like the += operator.
@user3923716 i complete my answers. You can found this syntax in lot of languages.
@RaphaelOliveira i'm not sure to understand your question. Explain me
2

stringWithFormat: works analogously to printf and the other C things that take formatter strings.

At various points in your string you've said to include %@. That means to take one object from the call stack and print it.

Printing the nil object is defined to produce (null).

Since you can't change how nil is printed, change what you're passing. E.g.

NSString *string =
    [NSString stringWithFormat:@"parameter=%@", parameter ? parameter : @""];

i.e. "if parameter is not nil then we'll pass parameter; otherwise we'll parse the empty string".

EDIT: if you want to omit parameters without values entirely, maybe build up the parameters you do want in an array or a dictionary and then form your URL at the end. E.g.

NSMutableArray *arguments = [NSMutableArray array];

if(value1)
    [arguments addObject:[NSString stringWithFormat:@"property1=%@", value1];

if(value2)
    [arguments addObject:[NSString stringWithFormat:@"property2=%@", value2];

... etc ...

// this will include the '?' even if arguments is empty; don't forget to deal
// with that in production code
NSString *URLString = [NSString stringWithFormat:@"http://service?%@", 
                                  [arguments componentsJoinedByString:@"&"]];

2 Comments

i don't know which parameter is getting passed, because scenario is view controller 1 has three buttons, like name, phone ,Email.those three buttons carry three parameters.If user clicks any button he will pass that particular parameter to View controller 2 which has only button connecting to php.
That's exactly what the second block of code deals with.
1

Excluding the empty parameters should do the trick. So, instead of this:

http://localhost:8888/userinfo.php?name=(null)&Email=(null)&phone=123445435

You want to end up with:

http://localhost:8888/userinfo.php?phone=123445435

You can achieve this by doing something like:

NSMutableArray *params = [NSMutableArray new];
if (name) [params addObject:[NSString stringWithFormat:@"name=%@", name]];
if (Email) [params addObject:[NSString stringWithFormat:@"Email=%@", Email]];
if (phone) [params addObject:[NSString stringWithFormat:@"phone=%@", phone]];

NSString *urlString = [NSString stringWithFormat:@"http://localhost:8888/userinfo.php?%@", [params componentsJoinedByString:@"&"]];

2 Comments

This isn't an answer unless you are going to provide how to do this.
yeah i know, the scenario is different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.