Sending Text to php!

Discussion in 'Mac Programming' started by nunikasi, Jun 26, 2008.

  1. macrumors newbie

    Joined:
    Jun 23, 2008
    #1
    Hi. Im trying to make a little application that sends text to a website.

    I will have a window.
    In this window ill make a text field.
    When i type "123" in that field and click send, i want it to send it to a php file so i can instantly accsess it on my webserver.

    Can someone please tell me how to do this?

    Thank you :)
     
  2. macrumors 6502a

    Joined:
    Nov 14, 2007
    #2
    Not sure on the Cocoa side of things (still learning) but you'll need to set up a PHP form with a GET method in it, then get your program to send the data like

    http://example.com/form.php?data=123

    Not the most secure way, but works most of the time.

    http://www.w3schools.com/PHP/php_get.asp

    Maybe you could use webkit or some command line (curl? wget?) to 'ping' the URL you want.
     
  3. macrumors 6502

    Joined:
    Apr 28, 2008
    #3
    NSMutableRequest's setHTTPMethod and setHTTPBody methods....
     
  4. thread starter macrumors newbie

    Joined:
    Jun 23, 2008
  5. macrumors 6502

    Joined:
    Apr 28, 2008
    #5
    This is going to be very basic as I don't really have time to make a full tutorial but this may help you get on your way.

    First create a PHP form that accepts POST data [HTTP command to upload content to a web server]

    Then do something along the lines of:

    Code:
    NSURL *url = [NSURL URLWithString: @"http://www.mysite.com/formThatSavesDataSomehow.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url];
    
    [request setHTTPMethod: @"POST"];
    [request setHTTPBody: [[[NSString alloc] initWithString: @"text=yourValueGoesHere"] dataUsingEncoding: NSASCIIStringEncoding]];
    
    NSURLConnection* theConnection = [NSURLConnection connectionWithRequest: request delegate: self];
    
    // Somehow invoke this connection? You'll have to figure this one out
    
    Then your PHP form can simply reference the POST variable $_POST["text"]. You can display that value back to the user, or you can save it to a database... I don't know what you're trying to accomplish here.

    [EDIT:] I just realized, if all you're trying to do is pass a value off to a PHP form, you could just build the url like so:

    Code:
    NSString *uri = [NSString stringWithFormat: @"http://www.mysite.com/formThatSavesDataSomehow.php?text=%@", textField.text];
    NSURL *url = [NSURL URLWithString: uri];
    NSURLRequest *request = [NSURLRequest requestWithURL: url];
    NSURLConnection* theConnection = [NSURLConnection connectionWithRequest: request delegate: self];
    
    Thereby saving you from having to deal with all that POST crap. Then in the PHP you pull it out of the request variable $_REQUEST["text"];

    HTH,

    Enuratique
     
  6. macrumors 6502

    Joined:
    Jun 16, 2004
    #6
  7. thread starter macrumors newbie

    Joined:
    Jun 23, 2008
    #7
    Thank you for the help.

    Here is my project folder, This

    I write something in the field, press enter and it shows bellow the field.
    I want it to show in the browser!

    Can someone help me with this?
    Just send me the new control.h/m you got from the link above.

    Thanks! :) :D
     
  8. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    No one is going to do this work for you. You got examples with code, when references to the classes involved would have been sufficient.

    I would recommend sticking with POST if you don't know the size of the entry. There are various limitations on the length of GET requests across different servers and browsers, but I don't know what it would be for this particular Obj-C class. POST is always safer as you can send much larger parameters, and technically the intent is that if you are changing state things should always be POST, and GET is only used for "reading" things with parameters (i.e. pulling up an auction listing based on id). This isn't really abided by, but it's still a decent rule of thumb.

    It's also not clear what your intention is. Do you want to just send something to a web server for storage, etc.? Or do you just want to open the URL in a browser control/window? Interface builder lets you embed a browser control in your own app that could be used. If you want to open a separate browser, look at NSWorkspace's openURL or
    Code:
    openURLs:withAppBundleIdentifier:options:additionalEventParamDescriptor:launchIdentifiers:
    .

    -Lee
     

Share This Page