0
爲什麼我的參數vistor_name
和vistor_email
不能通過PHP傳輸?爲什麼PHP代碼不會響應SWIFT中的UITextField參數?
當收身看起來是這樣的:
Name:
Email:
這是我的SWIFT代碼:
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var ageField: UITextField!
@IBAction func sendButton(sender: AnyObject) {
sendToPhp()
}
func sendToPhp() {
let session = NSURLSession.sharedSession()
let urlString = "http://sitename.com/fromswift.php"
let url = NSURL(string: urlString)
let request:NSMutableURLRequest = NSMutableURLRequest(URL: url!)
let parameter: NSString = NSString(string: "visitor_name=\(nameField.text), visitor_email=\(ageField.text)")
request.HTTPMethod = "POST"
request.HTTPBody = parameter.dataUsingEncoding(NSUTF8StringEncoding)
session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
print("done, error: \(error)")
}
print(bodyData)
}
,這是我的PHP代碼:
<?php
$msg = 'Name: ' .$_POST['visitor_name'] ."\n"
.'Email: ' .$_POST['visitor_email'] ."\n"
mail('[email protected]', 'FeedBack from: me', $msg);
?>
你可以試試$ msg = var_export($ _ POST,true); - 這將顯示發佈的變量(如果有的話)。如果它是空的,你可能需要添加:request.setValue(「application/x-www-form-urlencoded」,對於HTTPHeaderField:「Content-Type」) – Maltronic
@Goat Master我試過了,它是空的。我添加了:'request.setValue(「application/x-www-form-urlencoded」,對於HTTPHeaderField:「Content-Type」)'仍然是一樣的沒有什麼事 – hdjkshdak
你的參數字符串需要在'UITextField'變量周圍加上引號: 'visitor_name = \「\(nameField.text)\」' – RichAppz