I am trying to send my username to server. Hereby is my objective-c code:
NSURL *postUrl=[NSURL URLWithString:@"http://yatanadam.com/Nappsak/checkTheUser.php"];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:postUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0f];
NSString *strData=@"userName=yatanadam";
NSData* data = [strData dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d",[data length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];
NSURLResponse *response=nil;
NSError *error=nil;
NSData *result=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *string = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
if (error==nil) {
NSLog(@"%@",string);
}
And hereby my php code :
<?php
$userName = $_POST["userName"];
echo $userName ;
$con = mysql_connect("localhost","username","password");
if (!$con)
{
$hata= "hata";
echo hata;
die('Baglanti saglanamadi: ' . mysql_error());
}mysql_select_db("yata", $con);
$degisken= "Select * from Users where user_name=$userName";
$showresult = mysql_query($degisken);
$multi_array = array();
while($row = mysql_fetch_assoc($showresult)){
$multi_array[] = $row;}
print json_encode($multi_array);
?>}
I am on this issue for hours now.The question is i can't get correct response from server.My server code can't catch my post data (username). I sthere anybody that can help me where is my mistake guys ?
Your headers say you're sending application/json
(Content-Type), but the data you are actually sending is just a plain string ("userName=yatanadam").
What you probably want to be doing is sending application/x-www-form-urlencoded
(and it looks like that is what your PHP is expecting).