Posts

Post marked as solved
6 Replies
2.6k Views
I'm working on an app in xcode that allows users to enter information on a registration page. I'm very new to swift but I was wondering if it is possible for when the user is entering information in the username textfield, with each character the user types, it will call mysql database to check to see if the username is available or not. I am using php as the middle man between swift and mysql. However, I've struggled to get this code to work. Does anyone know if this is even possible? and if so, how can I correct my code to make it work? Thanks a lot in advance. My code is posted below:@IBAction func TextFieldEditingDidChange(_ sender: Any) { let request = NSMutableURLRequest(url: NSURL(string: "usernamecheck.php")! as URL) request.httpMethod = "POST" print("Request: \(request)") let postString = "username=\(usernameTxt.text!)" request.httpBody = postString.data(using: String.Encoding.utf8) let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in if error != nil { print("error=\(error)") return } // Ends errror If statements print("response = \(response)") let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) print("responseString = \(responseString)") } task.resume() }portion PHP file:$a_sql = mysqli_query($con, "SELECT username FROM users WHERE username = '" . mysqli_real_escape_string($_POST['username']) . "' ; ");if (empty($_POST['username'])) { $username_error = "Please input username"; } else { $a_sql; } if ($a_sql && mysqli_num_rows($a_sql) > 0) { $username_exists = "Username is already taken."; echo $username_exists; } else { echo "Fail"; }
Posted
by mikamitch.
Last updated
.