user3236149
user3236149

Reputation: 167

send parameters to web server

I have a code which sent the data to mail but the error is how sent it.

Here is the code:

var nombre: String = ""
var Email: String = ""

@IBOutlet weak var nombresCliente: UITextField!
@IBOutlet weak var correoCliente: UITextField!
@IBOutlet weak var telefonoCliente: UITextField!
@IBOutlet weak var mensajeCliente: UITextField!

@IBAction func enviarMensaje(sender: AnyObject) {

    let url = URL(string: "https://www.domainXXXX/enviarMensaje.php")!
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    let postString = "nCliente=\(nombresCliente)&cCliente=\(correoCliente)&tCliente=\(telefonoCliente)&mCliente=\(mensajeCliente)&nDoctor=\(nombre)&eDoctor=\(Email)"
    request.httpBody = postString.data(using: String.Encoding.utf8)
    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {

            // check for fundamental networking error
            print("error=\(String(describing: error))")
            return
        }

        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {

            // check for http errors
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(String(describing: response))")
        }

        let responseString = String(data: data, encoding: .utf8)
        print("responseString = \(String(describing: responseString))")
    }
    task.resume()

}

The data is sent to the mail... but is a swift code with it and I don't know how to delete it or hide.

Here is the data sent it:

Optional(<UITextField: 0x7f8832a7ea00; frame  (142 220; 217 30); text
'Joaquín Vides'; opaque  NO; autoresize  RM BM;
gestureRecognizers  <NSArray: 0x600000440180>; layer  <CALayer:
0x60000023b400>>)

All the data is similar... my question is: how only show the data not the other code.

Upvotes: 0

Views: 215

Answers (1)

vikingosegundo
vikingosegundo

Reputation: 52227

Instead of passing the texts (entered into the textfields) to the string, you pass in the textfields themselves. and you don't unwrap the data object.

let postString = "nCliente=\(nombresCliente.text!)…"
request.httpBody = postString.data(using: String.Encoding.utf8)!

Upvotes: 2

Related Questions