· 9 years ago · Apr 10, 2017, 07:10 AM
1<?php
2 require 'database/connect.php';
3 global $connect;
4 date_default_timezone_set('Etc/UTC');
5 require 'PHPMailer-master2/PHPMailerAutoload.php';
6
7 if ( isset($_POST['data1']) && isset($_POST['data2']))
8 {
9 $data1 = $_POST['data1'];
10 $data2 = $_POST['data2'];
11
12 $sql = "SELECT * FROM table WHERE data1 = '$data1' AND data2='$data2'";
13 $result = mysqli_query($connect, $sql);
14 if ($result && mysqli_num_rows($result) > 0)
15 {
16 while ($row = mysqli_fetch_array($result)){
17 }
18
19 $output = array('message' => '1');
20 echo json_encode($output);
21 $add = "INSERT INTO table (data1, data2)
22 VALUES ('$data1','$data2')
23 ";
24 $run = mysqli_query($connect,$add);
25 $mail = new PHPMailer;
26 $mail->isSMTP();
27 $mail->Host = 'smtp.gmail.com';
28 $mail->SMTPAuth = true;
29 $mail->Username = 'gmail.com';
30 $mail->Password = '******';
31 $mail->SMTPSecure = 'tls';
32 $mail->Port = 587;
33 $mail->setFrom('sender@mail.com', 'sender');
34 $mail->addAddress('receiver@mail.com','receiver');
35 $mail->isHTML(true);
36 $mail->Subject = 'Test';
37 $mail->Body = 'Test';
38 $mail->AltBody = 'Test';
39 if(!$mail->send())
40 {
41 echo 'Message could not be sent.';
42 echo 'Mailer Error: ' . $mail->ErrorInfo;
43 }
44 else
45 {
46 echo 'Message has been sent';
47 }
48 // End sending email
49 exit();
50
51 mysqli_free_result($result);
52 }
53 else {}
54 }
55?>
56
57@IBAction func postData(_ sender: Any) {
58 let url = URL(string: "http://localhost/send.php")
59 let session = URLSession.shared
60 let request = NSMutableURLRequest(url: url! as URL)
61 request.httpMethod = "POST"
62 let valueToSend = "data1=&data2"
63 request.httpBody = valueToSend.data(using: String.Encoding.utf8)
64 let myAlert = UIAlertController(title: "Title", message: "Sure ?", preferredStyle: UIAlertControllerStyle.alert)
65 let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: nil)
66 let okaction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler:
67 {
68 action in
69
70 let task = session.dataTask(with: request as URLRequest, completionHandler: {
71 (data, response, error) in
72 if error != nil { return }
73 else {
74 do {
75 if let json = try JSONSerialization.jsonObject(with: data!) as? [String: String]
76 {
77 DispatchQueue.main.async {
78 let message = Int(json["message"]!)
79 if(message == 1){
80 let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController
81 let appDelegate = UIApplication.shared.delegate as! AppDelegate
82 let navigationController = UINavigationController.init(rootViewController: myViewController)
83 appDelegate.window?.rootViewController = navigationController
84 appDelegate.window?.makeKeyAndVisible()
85
86 let myAlert = UIAlertController(title: "Success !", message: "Sent !", preferredStyle: UIAlertControllerStyle.alert)
87 myAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
88 navigationController.present(myAlert, animated: true, completion: nil)
89 return
90 }
91 else { return }
92 }
93 }
94 }
95 catch let parseError {
96 print("Parse error: (parseError.localizedDescription)")
97 }
98 }
99 })
100 task.resume()
101 }
102 )
103 myAlert.addAction(okaction)
104 myAlert.addAction(cancel)
105 self.present(myAlert, animated: true, completion: nil)
106 }
107}