1

This is my first question and I hope to get some help:

I've used several methods (and tried with different http transmission frameworks) to make a post action to a php script in localhost with no good results. I don't know what else to look for. I've been stuck with this for at least 6 or 7 hours and finally got to this:

Objective C code:

if (name != nil && message != nil) { NSMutableString *postString = [NSMutableString stringWithString:TFPostURL]; [postString appendString:[NSString stringWithFormat:@"%@=%@",TFName,name]]; [postString appendString:[NSString stringWithFormat:@"%@=%@",TFMessage,message]]; [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]]; [request setHTTPMethod:@"POST"]; postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; } 

TFPostURL, TFName, TFMessage, name and message are constants defined by me.

I'm using the database just to check if the transmission has been successfully done:

PHP Script

<?php $username = "root"; $database = "db"; mysql_connect(localhost, $username); @mysql_select_db($database) or die("Unable to find database"); $name = $_POST["name"]; $message = $_POST["message"]; $query = "INSERT INTO test VALUES ('', '$name', '$message')"; mysql_query($query) or die(mysql_error("error")); mysql_close(); ?> 

Any ideas on what would be wrong?

I've been tried always in localhost, still need to try this in another mac. I'm on my way to upload my php file to a public domain and check if it's something in my own computer.


Edit:

I've changed the PHP POST action to GET, obtaining amazing results; NOW IT WORKS!

<?php $username = "root"; $database = "db"; mysql_connect(localhost, $username); @mysql_select_db($database) or die("Unable to find database"); $name = $_GET["name"]; $message = $_GET["message"]; $query = "INSERT INTO test VALUES ('', '$name', '$message')"; mysql_query($query) or die(mysql_error("error")); mysql_close(); ?> 
3
  • Just so you know, localhost on the simulator should work - Safari on the simulator is able to access my mac's http server. Try opening your php script in the iOS Simulator's Safari browser and see if the database entry pops up there. If it does, then you can be certain it's the app code causing the issue, not the php. Commented Jan 26, 2012 at 4:16
  • Welcome to SO, Fernando! Commented Jan 26, 2012 at 4:17
  • Thanks guys! I've solved my problem... really, I never saw it coming this way. Check the Edit! Commented Jan 26, 2012 at 5:56

1 Answer 1

3

I recommend using the ASIHTTP libraries. They are excellent. Here's some example code:

ASIHTTPRequest* req = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"localhost"]] autorelease]; [req setCompletionBlock:^{ NSLog(@"It works! Response was %@",[req responseString]); }]; [req startAsynchronous]; 

It makes asynchronous responses a snap and lets you use closures like a boss.

I have tried dealing with the built-in NSURLRequest methods for a while and strongly prefer the simplicity of being able to make asynchronous requests without creating a separate delegate and methods to handle it. It also has support for POSTing data and files, as well as some other tricks I've even yet to use.

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

5 Comments

Hi Tim, I've already tried ASIHTTP Libraries with no good results although everyone is using them with no problem. This points that something is strangely wrong.
Are you certain your php code is working? Can you try the code above, but make your webpage simply have a simple string and verify that the simulator is receiving the response? What ISN'T working? The request altogether? The values being sent?
Used network debugger and things out went fine, started making changes in my php file and as you thought, that was the problem. Simply changed the HTTP POSTs to GETs and went through it. Silly right?
Awesome. Glad to have helped.
Please note that ASIHTTPRequest is not actively developed anymore. allseeing-i.com/%5Brequest_release%5D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.