距离我的应用程序的登录页面工作已近一天了,我想向我的应用程序显示错误(或者来自PHP的回声)到我的xCode应用程序。我将向您展示我的PHP文件和我的xCode
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$password = $_POST['password'];
$email = $_POST['email'];
if($password == '' || $email == '')
{
echo 'Please fill all values.';
}
else
{
require_once('GBconnect.php');
$sql = "SELECT * FROM Users WHERE email='$email' AND password='$password' OR mobile_no='$email' AND password='$password'";
$check = mysqli_fetch_array(mysqli_query($connection,$sql));
if(isset($check))
{
echo 'Login Success';
}
else
{
echo 'Email/Phone or Password is wrong.';
}
mysqli_close($connection);
}
}
else
{
echo 'error';
}
这是我的Swift文件:
@IBAction func signUp(_ sender: Any)
{
let request = NSMutableURLRequest(url: NSURL(string: "http://34.205.37.201/restapi/GBlogin3.php")! as URL)
request.httpMethod = "POST"
let logEmail = "email=\(username.text!)&& password=\(password.text!)"
let logMobile = "mobile_no=\(username.text!)&& password=\(password.text!)"
if (username.text?.isEmpty)! || (password.text?.isEmpty)!
{
//display message
LoginInfoMyAlertMessage(userMessage: "Please input your Email or Phone and Password.");
}
if let checkNum = Int(username.text!)
{
print(checkNum)
request.httpBody = logMobile.data(using: String.Encoding.utf8)
}
let task = URLSession.shared.dataTask(with: request as URLRequest)
{
data, response, error in
if error != nil
{
print("error=\(String(describing: error))")
}
print("response = \(String(describing: response))")
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("responseString = \(String(describing: responseString))")
}
task.resume()
username.text = ""
password.text = ""
return
答案 0 :(得分:0)
我看到的一些事情:
logEmail
但从未在任何地方使用logMobile
中有空格,但在使用application/x-www-form-urlencoded
POST数据时不应该尽管如此,您还没有指定运行代码时所看到的内容。如果你能做到这一点,我们可以提供更具体的建议。使用POSTMAN验证服务器端是否正常工作,然后确保客户端正在使用服务器。
答案 1 :(得分:0)
您可以对响应进行编码,然后在源应用程序中展开以处理不同的消息。
<?php
$password = $_POST['password'];
$email = $_POST['email'];
if($password != '' || $email != ''){
$check = false; //Your connection
if($check){
echo json_encode([
"Message" => "Login Success",
"Status" => "Ok"
]);
}
else{
echo json_encode([
"Message" => "Email/Phone or Password is wrong.",
"Status" => "Error"
]);
}
}
?>
然后
@IBAction func signup(_ sender: Any) {
let request = NSMutableURLRequest(url: NSURL(string: "http://localhost:8888/chava/login.php")! as URL)
request.httpMethod = "POST"
let logEmail = "email=\(emial.text!) && password=\(password.text!)"
print(logEmail)
if (emial.text?.isEmpty)! || (password.text?.isEmpty)!{
print("Please input your Email or Phone and Password.");
}
request.httpBody = logEmail.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
if error != nil{
print("error=\(String(describing: error))")
}else{
//print(String(data:data!,encoding: .utf8) ?? "")
if let resp = data {
do{
let jsonResult = try JSONSerialization.jsonObject(with: resp) as! [String:AnyObject]
if let message = jsonResult["Message"]{
print(message)
}
}catch{
}
}
}
//print("response = \(String(describing: response))")
//let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
//print("responseString = \(String(describing: responseString))")
}
我希望能帮到你!