开发者

POST and multiple submit buttons on form

开发者 https://www.devze.com 2022-12-29 20:52 出处:网络
NSString *reqURL = [NSString stringWithFormat:@\"%@/login\",SERVER_URL]; NSMutableURLRequest *req = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:reqURL]];
    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 r开发者_运维百科eally 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)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消