im trying to build a register page, but on clicking the button 'Register' nothing happen, getting this error:
[12:54:39] Network Error
- node_modules\axios\lib\core\createError.js:15:17 in createError
- node_modules\axios\lib\adapters\xhr.js:81:22 in handleError
- node_modules\event-target-shim\dist\event-target-shim.js:818:20 in EventTarget.prototype.dispatchEvent
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:600:10 in setReadyState
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:395:6 in __didCompleteResponse
- node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189:10 in emit
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 in callFunctionReturnFlushedQueue
* [native code]:null in callFunctionReturnFlushedQueue
I have seen similar problems like mine, but their solutions doesn't worked for me.
tried to change localhost to IP, tried to add to AndroidManifest.xml the rows:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
....
android:usesCleartextTraffic="true"
but nothing helps, im pretty frustrated can't figure out my problem.
react code:
import React,{useEffect,useState} from "react";
import {View,TextInput,Text,StyleSheet,Button} from "react-native";
import axios from "axios";
const RegistrationScreen = () => {
const [username,setUsername]=useState("");
const [email,setEmail]=useState("");
const [password,setPassword]=useState("");
const [isSubmit,setIsSubmit]=useState(false);
useEffect(()=> {
const authenticate = async() => {
axios.post(
"http://10.0.0.8:8080/project/register.php",
JSON.stringify({
email: email,
password: password,
username: username,
})
)
.then((response) => {
console.log(response);
setIsSubmit(false);
})
.catch( (err) => {console.log(err);
});
};
if (isSubmit) authenticate();
},[isSubmit]);
return (
<View style={styles.container}>
<TextInput
placeholder="Username"
style={styles.input}
autoCapitalize="none"
onChangeText={(text)=>setUsername(text)}
/>
<TextInput
placeholder="Password"
style={styles.input}
autoCapitalize="none"
onChangeText={(text)=>setPassword(text)}
secureTextEntry= {true}
/>
<TextInput
placeholder="Email"
onChangeText={(text)=>setUsername(text)}
style={styles.input}
/>
<Text>{username}</Text>
<View style={styles.buttonContainer}>
<Button title="Register" onPress={() => setIsSubmit(true) }/>
</View>
</View>
);
};
const styles=StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
input: {
paddingVertical: 5,
borderBottomColor: "#ccc",
borderBottomWidth: 1,
width: "55%",
},
buttonContainer: {
marginTop: 20,
},
});
export default RegistrationScreen;
php code :
<?php
$conn = mysqli_connect('localhost', 'root', '','project_db');
$json=file_get_contents('php://input'); // Takes raw data from the request
echo $json;
$obj=json_decode($json,true); // Converts it into a PHP object
$username=$obj['username'];
$password=$obj['password'];
$email=$obj['email'];
echo $username;
echo "hello";
$query="SELECT * from users where email = '{$email}'";
$query_output=mysqli_query($conn,$query) or die ("Query faild: ".mysqli_error($conn));
$count=mysqli_num_rows($query_output);
if($count==1) {
$arr=array("result"=>"email already present");
echo json_encode($arr);
} elseif($count==0) {
$query1="INSERT INTO `users` (`email`,`password`,`username`) VALUES ('{$email}','{$password}','{$username}')";
$query_output=mysqli_query($conn,$query1) or die ("Query faild: ".mysqli_error($conn));;
$arr=array('result'=>'ok');
echo json_encode($arr);
} else {
$arr=array('result'=>'fail');
echo json_encode($arr);
}
?>
help someone please ?