1
I’m trying to send my app registration data in React-Turn to my PHP api.
Follow my code React:
const handleSignUp = useCallback(async (data: object) => {
try {
await signUpValidation.validate(data, {
abortEarly: false,
});
const result = await api.post('/client.php', {
...data,
option: "register"
});
console.log(data)
console.log(result);
} catch (error) {
if (error instanceof Yup.ValidationError) {
const validationErrors = getValidationErrors(error);
formRef.current?.setErrors(validationErrors);
}
console.log(error);
}
}, []);
My api.ts file:
import axios from 'axios';
const api = axios.create({
baseURL: 'http://localhost:8888/Projects/Web/SmartTrafficDashboard/api',
});
export default api;
And here, my php:
<?php
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
ini_set('display_errors', true);
error_reporting(E_ALL);
include_once("con.php");
$pdo = conectar();
$data = file_get_contents("php://input");
$data = json_decode($data);
if($data){
$option = $data->option;
}else{
$option = $_GET['option'];
}
switch ($option) {
case 'register':
$name = $data->clientName;
$rg = $data->rg;
$cpf = $data->cpf;
$cnh = $data->cnh;
$email = $data->email;
$password = md5($data->password);
$carBrand = $data->carBrand;
$carModel = $data->carModel;
$licensePlate = $data->licensePlate;
$registerClient=$pdo->prepare("INSERT INTO client (idclient, name, rg, cpf, cnh, email, password) VALUES(?,?,?,?,?,?,?)");
$registerClient->bindValue(1, NULL);
$registerClient->bindValue(2, $name);
$registerClient->bindValue(3, $rg);
$registerClient->bindValue(4, $cpf);
$registerClient->bindValue(5, $cnh);
$registerClient->bindValue(6, $email);
$registerClient->bindValue(7, $password);
$registerClient->execute();
$idclient = $pdo->lastInsertId();
$registerVehicle=$pdo->prepare("INSERT INTO clientvehicle (idclientvehicle, idclient, brand, model, licenseplate) VALUES(?,?,?,?,?)");
$registerVehicle->bindValue(1, NULL);
$registerVehicle->bindValue(2, $idclient);
$registerVehicle->bindValue(3, $carBrand);
$registerVehicle->bindValue(4, $carModel);
$registerVehicle->bindValue(5, $licensePlate);
$registerVehicle->execute();
break;
default:
# code...
break;
}
And the error that appears on the React Metro console is
[Error: Network Error]
NOTE: I tested via Postman, in api, and the registration worked.
Put fixed ip and try by ip not as localhost, it worked for me
– Leo Alves