views:

107

answers:

1
    NSString *reqURL = [NSString stringWithFormat:@"%@/login",SERVER_URL];

    NSMutableURLRequest *req = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:reqURL]];
    [req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];


    NSData *myRequestData = [NSData dataWithBytes:[@"username=whatever&password=whatever" UTF8String] length: [@"username=whatever&password=whatever" length]];
    [req setHTTPMethod: @"POST"];
    [req setHTTPBody: myRequestData];
    NSData *returnData = [NSURLConnection sendSynchronousRequest:req returningResponse: nil error: nil];
    NSString *html = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

I got this code from another question I asked. But what happens if there is more than one submit button. I really have no idea how to ask this question. An example of such situation is on the logout page for this site. There are no fields to enter data into, but there are 2 submit buttons.

How can I "simulate clicking" on one of those buttons using code like the above (so not using a UIWebView)

A: 

The way those forms usually tell which button was pressed is by naming their buttons and checking the value. Simply put, a basic page receiving form data will check if a form was even submitted by checking if ($_POST['submit'] == "Send!") which tells the page that the user got there by pressing the button. The same concept is used when deciding what button was pressed.

if ($_POST["submit"] == "Send!") addDataToDB();
else if ($_POST["submit"] == "Update!") updateUser();
else if ($_POST["submit"] == "Remove me!") removeUser();

So now, what you need to do is check the source of the html page with the form and find out the name and value of the submit button you want to simulate and add that data to your POST body data in your request

Update: Oops! misunderstood your question, thought you meant multiple submit buttons in one form on one page, but now i think you meant one form going to another "confirmation" form...In your didRecieveData delegate method you will need to store all the html you get and in your didFinishLoading delegate method you will need to pull out any hidden field names & values and then create a new request with them as your POST data and the url being the "action" url of the form

casey
What happens if one submit button has a value and one doesn't?
Jonathan
well if you are trying accommodate multiple submit buttons on a single form then you would still just add the button name and value onto your POST data. even if the value is nothing. As long as the variable is set
casey
"thought you meant multiple submit buttons in one form on one page" No that is what I meant :)
Jonathan