I tried so send some data from two textfields with my button, once i press the button I receive an E-mail without the content of the textfields which I typed in my iOS Simulator. I can not find the problem, please help me.
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
UITextField * firstname;
UITextField *lastname;
UIButton *sendPost;
}
@property (strong, nonatomic) IBOutlet UITextField *firstname;
@property (strong, nonatomic) IBOutlet UITextField *lastname;
@property (strong, nonatomic) IBOutlet UIButton *postButtonTapped;
-(IBAction)postButtonTapped:(id)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize firstname;
@synthesize lastname;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)postButtonTapped:(id)sender
{
NSString *myRequestString = [NSString stringWithFormat:@"firstname=%@&lastname=%@",firstname.text,lastname.text];
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://textfake.com/test/mail.php"]];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody: myRequestData];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response);
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.firstname resignFirstResponder];
[self.lastname resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
PHP Code
<?php
header("Content-type: text/html; charset=UTF-8");
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$text = "not set";
if(!empty($_POST["text"])){
$text = $_POST["text"];
}
$lat = $_POST["lat"];
$lon = $_POST["lon"];
$dieMessage =$firstname." ".$lastname."\n".$text."\n";
$dieMessage .="Lat:".$lat."\n";
$dieMessage .="Lon:".$lon;
mail("fortesing@gmail.com", "gps daten", $dieMessage);
//echo 'Email abgeschickt!';
echo $dieMessage;
?>
AFNetworking 2.0
Hope this works.
I solved the problem, seems like it had some problems with:
and it should be with www. instead:
for the iOS part, try this :
For the PHP part, you could not get text, lat, lon as you never post it !