Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

ASIHTTPREQUEST post/get question

justine80justine80 Posts: 10Registered Users
edited July 2011 in iPhone SDK Development
how do I setup the following using asihttprequest:

user inputs first name in iphone app
php file gets the first name data from the app
php file queries the db and outputs the 'last name' corresponding to that first name, into the app

Can't seem to find a mysql/php example using an iPhone input and asihttprequest

Thanks!!!
Post edited by justine80 on

Replies

  • dany_devdany_dev Posts: 4,696Tutorial Authors, Registered Users @ @ @ @ @
    edited July 2011
    break it down:
    1- make the php script that take the input (using get or post as you prefer), do the query and give an output in xml or json.
    2- see ASIHTTPRequest documentation to understand how to do a http request
    3- see how to parse xml (NSXMLParser) or JSON (SBJson)
  • justine80justine80 Posts: 10Registered Users
    edited July 2011
    dany_dev wrote: »
    break it down:
    1- make the php script that take the input (using get or post as you prefer), do the query and give an output in xml or json.
    2- see ASIHTTPRequest documentation to understand how to do a http request
    3- see how to parse xml (NSXMLParser) or JSON (SBJson)

    Thanks Dany_dev!
    With the ASIHTTPRequest, I've found the webservices doc on here, including other examples, however, I cannot seem to find an example which utilises an input box with a submit button, then outputs in the iphone view?

    Thank you again.
  • dany_devdany_dev Posts: 4,696Tutorial Authors, Registered Users @ @ @ @ @
    edited July 2011
    as already said you can use GET (inserting the variable directly in the link) or using POST.....

    read entirely WebService [How-To]
  • SpeedSpeed Posts: 615Registered Users
    edited July 2011
    ASIHTTPRequest contains a "responseString" property which allows you to see what the browser outputted back. For something like this, which I do not think is private data, you should be able to use simple GET over POST.
    NSURL *url = [NSURL URLWithString:[[NSString stringWithFormat:@"http://www.mycoolwebsite.com/mycoolscript.php?first_name=%@",first_name] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
            [request startSynchronous];
            NSError *error = [request error];
            NSString *response = nil;
            if (!error) {
                
                response = [request responseString];
                
                NSLog(@"%@",response);
                
            } else {
                
                return NO;
                
            }
    

    From there, you send a variable named "first_name" and your server will receive it as $_GET; Here could be an example of your PHP script.

    [php]include('database.php'); // Standard DB file

    $first_name = mysql_real_escape_string($_GET);

    $query = mysql_query("SELECT * FROM `names` WHERE `first_name` = '$first_name';");
    if (mysql_num_rows($query) == 0) {
    die('No results found for name: ' . $first_name);
    }
    $result = mysql_fetch_array($query);
    echo $result;[/php]

    Now, the script will echo the last_name value, and you will receive this iPhone side through the responseString property.
  • justine80justine80 Posts: 10Registered Users
    edited July 2011
    Thanks guys - maybe I should start again and explain why I'm having so much difficulty.

    I followed the instructions with setting up a new project and importing all the classes.

    I have now created a new view controller (.m and .h files)

    From there, once I run the application, I want the user to be shown a textbox and submit button. Once they enter text and hit submit, the response is displayed below.

    I have no problem setting up the PHP/MySQL end of things, it's the ASIHTTPRequest bit.

    Once my project is setup and blank, where to from there?
    If I copy an example from the site (ASIHTTPRequest example code - All-Seeing Interactive) it brings up a bunch of errors.

    Do I also need to use design view to add in the textbox and button or will the ASIHTTPRequest examples display them, using code?

    Thanks for your help and your patience with me!
  • dany_devdany_dev Posts: 4,696Tutorial Authors, Registered Users @ @ @ @ @
    edited July 2011
    the examples doesn't conain errors, so if so, probably you missed some import, refollow the "Setup Instructions"
Sign In or Register to comment.