Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8534029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:06:03+00:00 2026-06-11T10:06:03+00:00

I have a method that should post some data to a PHP file: -(void)submitForm

  • 0

I have a method that should post some data to a PHP file:

-(void)submitForm {
    NSLog(@"name=%@", formName.text); // returns correct value

    NSString *post = [NSString stringWithFormat:@"name=%@&", formName.text];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://path/to/file"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLResponse *response = NULL;
    NSError *requestError = NULL;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    NSLog(@"%@",responseString);
}

Note: I’m not actually using @"http://path/to/file". I’ve omitted the URL for privacy.

I believe it’s connecting to the PHP script correctly, since I’m receiving the response I expect. The problem is that if I echo out $name, I get an empty string. Here’s the script:

<?php
include("config.php"); // Handles DB connection
$name = mysql_escape_string($_POST["name"]);
mysql_query("insert into objects(name) values('$name')") or die(mysql_error());
echo $name; // returns empty string

All I expect is some sort of syntax/logic error in the Obj-C code, but I can’t spot it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:06:04+00:00Added an answer on June 11, 2026 at 10:06 am

    I’ve got a feeling that your request is missing some mandatory data, a boundary and content-type, which might lead to your POST request being half complete.

    I’ll show you what I’ve done for uploading an HTML file to a webserver and then explain underneath.

    Objective-C

    NSString* str = formName.text;
    NSData* theData = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSString *urlString = @"http://whereyouarepostingto.com";
    
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];  
    [request setURL:[NSURL URLWithString:urlString]];  
    [request setHTTPMethod:@"POST"];  
    
    NSString *boundary = @"---------------------------14737809831466499882746641449";  
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];  
    
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    
    NSMutableData *body = [NSMutableData data];  
    
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary]   dataUsingEncoding:NSUTF8StringEncoding]];  
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat: @"Content-Disposition: form-data; name=\"file\"; filename=\"%@.html\"\r\n", self.fileName]] dataUsingEncoding:NSUTF8StringEncoding]];  
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:theData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
    [request setHTTPBody:body];  
    
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];  
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
    
    NSLog(@"%@", returnString);
    

    PHP

       <?php
    
        echo "Connection made to server";
    
        move_uploaded_file($_FILES["file"][tmp_name], "uploads/" . $_FILES["file"]["name"]);  
    
        ?>
    

    You’ll need to change a few things around to fit your SQL insert and strip out my file stuff, but it’s a start.

    This is the part I think you’re missing.

    NSString *boundary = @"---------------------------14737809831466499882746641449";  
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    

    The boundary can be a series of random numbers, but it has to exist.

    Also, watch out when constructing the body of the request, especially the slashes. Make sure you escape any ” “.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small issue with a method that should save the text contained
I have a method that returns lot of data, should I use @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) for
I have this javascript that pulls some data via an ajax request the method
In my javascript file, I have a code snippet like: $('<form action=process.php method=POST>' +
I have a PHP script that uploads a CSV file and then saves some
On the page 'selecteditems.php' I have a form like this: <form method=POST name=selecteditems action=nextpage.php>
I have a python webapp which accepts some data via POST. The method which
I have created a method that should ideally take a single Account object and
I know that I should have schema of a table before calling NewRow method
I have method that returns module path of given class name def findModulePath(path, className):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.