发送请求到PHP服务器 [英] send Post request to PHP Server
本文介绍了发送请求到PHP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试过使用按钮从两个文本字段发送一些数据,一旦按下按钮,我将收到一封电子邮件,其中没有我在iOS模拟器中键入的文本字段的内容。我找不到问题,请帮助我。
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
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
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代码
<?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;
?>
推荐答案
我解决了这个问题,好像有一些问题
I solved the problem, seems like it had some problems with:
[NSURL URLWithString: @"http://textfake.com/test/mail.php"]];
,应该与www一起使用。代替:
and it should be with www. instead:
[NSURL URLWithString: @"http://www.textfake.com/test/mail.php"]];
这篇关于发送请求到PHP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文