Good evening, I have a problem with the json step by ajax, what happens, is that I am sending a json, so that dynamic rendering of html elements with js, came doing the step of this in the same way, the novelty is that apparently the notifications, large blanks, and dangers of the php code is happening as part of my json, and personally I have been trying for two days to solve it and I do not succeed, please if anyone knows any way to pass Just json what I want, please appreciate your help, this is the code:
code php create.php
<?php
header('Content-Type: application/json; charset=utf-8');
function request_ajax(){
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
}
$directorio=" /../../../images/artist/carpeta";
$temp = explode(".", $_FILES["image_principal"]["name"]);
ini_set('precision', 17);
$newfilename = (microtime(true) . '.' . end($temp));
$image1=(((float)$newfilename) + 1.3) * mt_getrandmax();
if(move_uploaded_file($_FILES['image_principal']['tmp_name'], __DIR__ . $directorio . $image1. $_FILES["image_principal"]["name"])){
$imagen_urlprincipal= 'artist/'.$image1 . $_FILES["image_principal"]["name"] ;
}
$temp1 = explode(".", $_FILES["image_biography"]["name"]);
ini_set('precision', 17);
$newfilename1 = microtime(true) . '.' . end($temp1);
$image2=((float)$newfilename1) * mt_getrandmax();
if(move_uploaded_file($_FILES['image_biography']['tmp_name'], __DIR__ . $directorio . $image2. $_FILES["image_biography"]["name"])){
$imagen_urlbiography= 'artist/'.$image2 . $_FILES["image_biography"]["name"] ;
}
$name=htmlspecialchars($_POST['name']);
$bio=htmlspecialchars($_POST['biography']);
$birt=htmlspecialchars($_POST['birthdate']);
$deat=htmlspecialchars($_POST['deathdate']);
try {
require_once('../../functions/conection.php');
$statement="insert into `artist` values ( null , '{$name}' , '{$imagen_urlprincipal}' , '{$bio}' , '{$birt}' ,'{$deat}', '{$imagen_urlbiography}');";
$result= $conection->query($statement);
if (request_ajax()) {
echo json_encode(array(
'result'=>$result,
'name'=> $name,
'biography'=> $bio,
'birthDate'=> $birt,
'deathDate'=> $deat,
'id_register'=> $conection->insert_id
));
}else {
exit;
}
} catch (Exception $e) {
}
?>
function js with ajax.
function createRegister(){
var data= new FormData(form);
for([key,value] of data.entries()){
console.log(key+": "+value);
}
var xhr= new XMLHttpRequest();
xhr.open('POST', action, true);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.onreadystatechange=function(){
if (xhr.readyState == 4 && xhr.status == 200) {
var result= xhr.responseText;
console.log("Resultado: "+ result );
var json= JSON.parse(result);
if(json.result==true){
div_create(json.name);
template(json.name,json.biography,json.birthDate, json.deathDate,json.id_register);
totalRegistros.innerHTML=parseInt(totalRegistros.textContent)+1;
}
}
}
xhr.send(data);
}
Error, from console of navegator.
name: test2
main.js:151 image_principal: [object File]
main.js:151 biography: xxxxxxxxxxxxxxxxxxxxxxxx
main.js:151 birthdate: 2018-08-18
main.js:151 deathdate: 2018-08-25
main.js:151 image_biography: [object File]
main.js:159 Resultado:
?>
{"result":true,"name":"test2","biography":"xxxxxxxxxxxxxxxxxxxxxxxx","birthDate":"2018-08-18","deathDate":"2018-08-25","id_register":31}
VM157:6 Uncaught SyntaxError: Unexpected token ? in JSON at position 11
at JSON.parse (<anonymous>)
at XMLHttpRequest.xhr.onreadystatechange (main.js:160)
what I do not understand is because the json comes with so many spaces and the symbol '?>' if I'm giving it the bd data structure, this is the first time it happens to me, and I do not understand that ajax is supposed to clean by default memory, so I do not think that's it, personally I'm lost.