Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138134

React Native Expo - can't send data to php file

$
0
0

I'm on React Native Expo and i try to build a user login with data from an existing MySql database. I can connect to the database and if I have a hardcoded username and password in my php file, I get a correct feedback back to my app.

My Login Class (the important part)

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}
        />

My 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();

?>

The part

$json = file_get_contents('php://input');

doesn't seem to work. There's no data coming from the input fields. Do you have any idea how can I send my text from the inputfields to the phpfile?


Viewing all articles
Browse latest Browse all 138134

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>