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 t
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
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";
}
?>
$_POST
is an array, not a function. You need square brackets to access array indices:
$newShift = $_POST['shift'];
$bartenderUsername = $_POST['username'];