6

I have done this several times before but for some reason I can't get the post to go through... I tried the php script with the variables set to _POST and without... When they aren't set to post it works fine. Here is my iOS code:

NSDate *workingTill = timePicker.date;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];
NSString *time = [formatter stringFromDate:workingTill];

NSString *post = [NSString stringWithFormat:@"shift=%@&username=%@", time, usernameString];

NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [post length]];

NSURL *url = [NSURL URLWithString:@"http://wowow.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSLog(@"%@", post);
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];

[NSURLConnection connectionWithRequest:request delegate:nil];

[self.navigationController popToRootViewControllerAnimated:YES];

And here is a chunk of the php, are the POST variables not in the right location?

<?php
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
    $arrData = array();

    // if input is object, convert into array
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }

    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index => $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}

    $newShift = $_POST('shift');
    $bartenderUsername = $_POST('username');

    mysql_connect("host", "name", "pw") or die(mysql_error());  
    mysql_select_db("harring4") or die(mysql_error());

    $result = mysql_query("SELECT * FROM BartenderTable WHERE username='".$bartenderUsername."'") or die(mysql_error());  

    $row = mysql_fetch_array($result);
    $newfname = $row['fname'];

I imagine this is a rather simple answer to a more experience developer, thanks for your help!

1
  • Out of curiosity, where were you running this PHP script? Meaning where was the file stored? Did you make your own dedicated server running PHP on it? Commented Oct 1, 2013 at 19:55

3 Answers 3

8

$_POST is an array, not a function. You need square brackets to access array indices:

$newShift = $_POST['shift'];
$bartenderUsername = $_POST['username'];
Sign up to request clarification or add additional context in comments.

Comments

3

Use following code. make sure that post string's receipt is a key and will be used in server. client side code

 NSString *receipt1 = @"username";

    NSString *post =[NSString stringWithFormat:@"receipt=%@",receipt1];
   NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:@"http://localhost:8888/validateaction.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSHTTPURLResponse* urlResponse = nil;
    NSError *error = [[NSError alloc] init];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"Response Code: %d", [urlResponse statusCode]);

    if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300)
    {
        NSLog(@"Response: %@", result);
    }

}

server side php script.

validation.php

<?php
 if(_POST)
    {
        if($_POST['receipt'] == 'username')
        {
            echo "post successfull";
         $receipt   = $_POST['key1'];
         echo $receipt;
        }
        else
    {
        echo "not post";
    }
?>

1 Comment

Dear @BHUPI, Can you take have a look on my this question please? I am facing the same problem. Here is the link : stackoverflow.com/questions/23321570/… Thanks in advance.
0

As I am a iPhone developer, I can say that your Objective-C code is correct. Also ensure that the data you are sending is not empty by

NSLog(@"%d,[postData length]");

it should not print 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.