【发布时间】:2019-11-13 10:51:43
【问题描述】:
我在 React Native Expo 上,我尝试使用现有 MySql 数据库中的数据构建用户登录。我可以连接到数据库,如果我的 php 文件中有硬编码的用户名和密码,我会收到正确的反馈给我的应用程序。
我的登录类(重要部分)
class LoginActivity extends Component {
// Setting up Login Activity title.
static navigationOptions =
{
title: 'LoginActivity',
};
constructor(props) {
super(props)
this.state = {
username: '',
password: '',
isLoading: true
}
}
UserLoginFunction = async () => {
const { username } = this.state ;
const { password } = this.state ;
let body = JSON.stringify({username, password})
const formData = new FormData();
formData.append('username', username);
formData.append('password', password);
console.log("formData: " + body);
await fetch('https://example.com/User_Login.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
//'Content-Type': 'application/json',
'Content-Type': 'multipart/form-data'
},
body: formData
/* JSON.stringify({ // Here's the fun part. Put your data here.
"username": this.state.username,
"password": this.state.password
}) */
}).then((response) => response.text()) //.json()
.then((responseJson) => { console.log("response: " + responseJson);
// If server response message same as Data Matched
if(responseJson === 'ok')
{
alert('YEAH!');
//Then open Profile activity and send user email to profile activity.
//this.props.navigation.navigate('Home', { username: username });
}
else{
Alert.alert(responseJson);
}
}).catch((error) => {
console.error(error);
});
}
render() {
return (
<View style={styles.MainContainer}>
<Text style= {styles.TextComponentStyle}>Login</Text>
<TextInput
placeholder="Enter User Name"
onChangeText={username=>this.setState({username})}
underlineColorAndroid='transparent'
style={styles.TextInputStyleClass}
/>
<TextInput
// Adding hint in Text Input using Place holder.
placeholder="Enter User Password"
onChangeText={TextInputValue=>this.setState({password:TextInputValue})}
underlineColorAndroid='transparent'
style={styles.TextInputStyleClass}
secureTextEntry={true}
/>
我的User_Login.php
<?php
header("allow-control-access-origin: *, Content-Type: application/json");
include 'DBConfig.php';
// Creating connection.
$conn = new mysqli($HostName,$HostUser,$HostPass,$DatabaseName);
//echo json_encode($con);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$json = file_get_contents('php://input'); // this part not seems to work.
$obj = json_decode($json,true);
// Populate User email from JSON $obj array and store into $email.
$username = $obj['username'];
//$username = $obj['opb'];
//$username = 'opb';// Populate Password from JSON $obj array and store into $password.
$password = $obj['password'];
//$password = '123456';
//Applying User Login query with email and password match. username password
$sql = ("SELECT * FROM fe_users_app where username='$username' and password='$password'");
// Executing SQL Query.
$result = $conn->query($sql);
if($username != null){
if($result->num_rows==0){
echo json_encode('Wrong Details');
}
else{
echo json_encode('ok');
}
}
else{
echo json_encode('Keine Daten');
}
$conn->close();
?>
部分
$json = file_get_contents('php://input');
似乎不起作用。没有来自输入字段的数据。
您知道如何将我的文本从inputfields 发送到php 文件吗?
【问题讨论】:
标签: javascript php html react-native expo