I am pretty new to php and js, I saw the similar posts but they were in different languages like react or nodejs, which I haven’t studied yet and thus I don’t understand.
How and why am I getting redirected since I wrote in my php page to send back a json?
How can I get a json in order to extract the location I decided?
Javascript:
const register_endpoint ="../apis/user/register.php";
function isFormCorretto(event){
event.preventDefault();
//the three functions check if the fields are correct before sending the form, they work as intended
if(isNomeCorretto() && isPassswordCorretta() && isRipetiCorretta()){
//send form from here to api for Login;
const form = new FormData();
form.append('nome_utente', nome_utente.value);
form.append('password', password.value);
form.append('ripeti_password', ripeti_password.value);
fetch(register_endpoint, {
method:'post',
body: form
}).then(onResponseRegister);
}
}
function onResponseRegister(response){
console.log("response di registrazione ricevuta:");
console.log(response);
return response.json().then(onJsonRegister);
//stuck ^here^ showing the aforementioned error
}
function onJsonRegister(json){
console.log("json di registrazione ricevuto:");
console.log(json);
if(json.ok){
console.log(document.location.href);
window.location.replace("../"+json.new_location+"/");
}
}
And PHP:
<?php
require "../../session/session.php";
header('Content-Type: application/json; charset=utf-8');
if(!$idUtente=isLogged()){
header("Location: ../../index/");
exit;
}
if(!empty($_POST["nome_utente"]) && !empty($_POST["password"]) && !empty($_POST["ripeti_password"])){
$dbconn=mysqli_connect($dbsettings["host"], $dbsettings["username"], $dbsettings["password"], $dbsettings["dbname"]) /*or die(mysqli_error($dbconn))*/;
if(mysqli_connect_errno()){
echo json_encode(array(
'ok'=>false,
'error'=>'database_connection'
));
exit();
}
if(!preg_match("/^[a-zA-Z0-9_]{3,15}$/", $_POST["nome_utente"])){
echo json_encode(array(
'ok'=>false,
'error'=>'invalid_username'
));
mysqli_close($dbconn);
exit;
}
else {
$nomeUtente = mysqli_real_escape_string($dbconn, $_POST["nome_utente"]);
$query = "SELECT nome_utente FROM utenti WHERE nome_utente = '$nomeUtente'";
$risultato = mysqli_query($dbconn, $query);
if(mysqli_num_rows($risultato) != 0){
echo json_encode(array(
'ok'=>false,
'error'=>'dupe_username'
));
mysqli_close($dbconn);
exit;
}
if(strlen($_POST["password"])>15 || strlen($_POST["password"])<10){
echo json_encode(array(
'ok'=>false,
'error'=>'invalid_password'
));
mysqli_close($dbconn);
exit;
}
if(strcmp($_POST["password"], $_POST["ripeti_password"])!=0){
echo json_encode(array(
'ok'=>false,
'error'=>'not_matching_passwords'
));
mysqli_close($dbconn);
exit;
}
mysqli_free_result($risultato);
$nomeUtente = mysqli_real_escape_string($dbconn, $_POST["nome_utente"]);
$password = mysqli_real_escape_string($dbconn, $_POST["password"]);
$password = password_hash($password, PASSWORD_DEFAULT);
$query = "INSERT INTO Utenti(nome_utente, password) VALUES ('$nomeUtente', '$password')";
if(mysqli_query($dbconn, $query)){
$_SESSION["nome_utente"] = $_POST["nome_utente"];
$_SESSION["id_utente"] = mysqli_insert_id($dbconn);
echo json_encode(array(
'ok'=>true,
'new_location'=>'private'
));
mysqli_close($dbconn);
exit;
} else {
echo json_encode(array(
'ok'=>false,
'error'=>'database_connection'
));
mysqli_close($dbconn);
exit;
}
}
} else {
echo json_encode(array(
'ok'=>false,
'error'=>'missing_data'
));
exit;
}
?>
what I get is the private area of the user (the whole PHP/HTML) instead of the expected json [‘ok’=>true,’new_location’=>’private’]
Matt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.