The problem is that when I try to save the query sql in a PHP array and then send it as JSON, nothing is sent or a single record is sent. I show you the code.
$("#btn-send").click(()=>{
var datos_to_send={data:"estados"}; //Esta informacion se recibe correctamente en el post del archivo PHP
console.log("mostrando valor de texto "+datos_to_send.data);
$.ajax({
url:"js/link.php",
type:"POST",
dataType:"JSON",
data:datos_to_send,
async:false,
beforesend:(something)=>{
alert('Inicia AJAX');
},
success : (json)=> {
alert('Exito! Funciona bien ');
},
error : (xhr, status)=> {
alert('Disculpe, existió un problema');
console.log("Error "+status);
console.log("Error nro"+xhr);
},
complete : (xhr, status)=> {
alert('Finaliza AJAX');
}
}).done((recibido)=>{
alert('Exitoooo'+recibido);
console.log(recibido);
})
})
This JS file seems to work correctly when sending the information via POST.
<?php
$data_received = $_POST["data"]; //Recibe bien la informacion
$host="localhost";
$port=3306;
$user="root";
$dbname="extras";
$conn = mysqli_connect($host, $user, $password, $dbname);
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM estados";
$result = mysqli_query($conn, $sql) or die(mysqli_error($con));
//Hasta este punto el codigo funciona correctamente
$array_to_send=array();
$i=0;
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$array_to_send[$i] = $row;
//El problema se muestra en esta linea,
//Si ejecuto el codigo asi no envia nada
//Se muestra código 200 pero ninguna información
//Simplemente envia NADA
$i++;
$array_to_send = $row;
//Pero si le quito los corchetes y el indice
//De esta manera me envia correctamente 1 solo registro
//Generalmente el ultimo
}
}
else
{
echo json_encode("nada por aqui", JSON_FORCE_OBJECT);
}
mysqli_close($conn);
echo json_encode($array_to_send);
die();
?>
As I wrote in the comments, the code. I do not understand what happens, the only way to send the information in a "relatively" correct way is to remove the index of the variable that stores the queries, but there are many records that I must send. Can someone explain to me the correct way to send PHP records to JS in a fix?
Thank you very much in advance
After painting the result of the arrangement with print_r($array_to_send);
the following is shown
Array
(
[0] => Array
(
[0] => 1
[id_estado] => 1
[1] => Amazonas
[estado] => Amazonas
[2] => VE-X
[iso_3166-2] => VE-X
)
[1] => Array
(
[0] => 2
[id_estado] => 2
[1] => Anzo�tegui
[estado] => Anzo�tegui
[2] => VE-B
[iso_3166-2] => VE-B
)
[2] => Array
(
[0] => 3
[id_estado] => 3
[1] => Apure
[estado] => Apure
[2] => VE-C
[iso_3166-2] => VE-C
)
[3] => Array
(
[0] => 4
[id_estado] => 4
[1] => Aragua
[estado] => Aragua
[2] => VE-D
[iso_3166-2] => VE-D
)
[4] => Array
(
[0] => 5
[id_estado] => 5
[1] => Barinas
[estado] => Barinas
[2] => VE-E
[iso_3166-2] => VE-E
)
[5] => Array
(
[0] => 6
[id_estado] => 6
[1] => Bol�var
[estado] => Bol�var
[2] => VE-F
[iso_3166-2] => VE-F
)
[6] => Array
(
[0] => 7
[id_estado] => 7
[1] => Carabobo
[estado] => Carabobo
[2] => VE-G
[iso_3166-2] => VE-G
)
[7] => Array
(
[0] => 8
[id_estado] => 8
[1] => Cojedes
[estado] => Cojedes
[2] => VE-H
[iso_3166-2] => VE-H
)
[8] => Array
(
[0] => 9
[id_estado] => 9
[1] => Delta Amacuro
[estado] => Delta Amacuro
[2] => VE-Y
[iso_3166-2] => VE-Y
)
[9] => Array
(
[0] => 10
[id_estado] => 10
[1] => Falc�n
[estado] => Falc�n
[2] => VE-I
[iso_3166-2] => VE-I
)
[10] => Array
(
[0] => 11
[id_estado] => 11
[1] => Gu�rico
[estado] => Gu�rico
[2] => VE-J
[iso_3166-2] => VE-J
)
[11] => Array
(
[0] => 12
[id_estado] => 12
[1] => Lara
[estado] => Lara
[2] => VE-K
[iso_3166-2] => VE-K
)
[12] => Array
(
[0] => 13
[id_estado] => 13
[1] => M�rida
[estado] => M�rida
[2] => VE-L
[iso_3166-2] => VE-L
)
[13] => Array
(
[0] => 14
[id_estado] => 14
[1] => Miranda
[estado] => Miranda
[2] => VE-M
[iso_3166-2] => VE-M
)
[14] => Array
(
[0] => 15
[id_estado] => 15
[1] => Monagas
[estado] => Monagas
[2] => VE-N
[iso_3166-2] => VE-N
)
[15] => Array
(
[0] => 16
[id_estado] => 16
[1] => Nueva Esparta
[estado] => Nueva Esparta
[2] => VE-O
[iso_3166-2] => VE-O
)
[16] => Array
(
[0] => 17
[id_estado] => 17
[1] => Portuguesa
[estado] => Portuguesa
[2] => VE-P
[iso_3166-2] => VE-P
)
[17] => Array
(
[0] => 18
[id_estado] => 18
[1] => Sucre
[estado] => Sucre
[2] => VE-R
[iso_3166-2] => VE-R
)
[18] => Array
(
[0] => 19
[id_estado] => 19
[1] => T�chira
[estado] => T�chira
[2] => VE-S
[iso_3166-2] => VE-S
)
[19] => Array
(
[0] => 20
[id_estado] => 20
[1] => Trujillo
[estado] => Trujillo
[2] => VE-T
[iso_3166-2] => VE-T
)
[20] => Array
(
[0] => 21
[id_estado] => 21
[1] => Vargas
[estado] => Vargas
[2] => VE-W
[iso_3166-2] => VE-W
)
[21] => Array
(
[0] => 22
[id_estado] => 22
[1] => Yaracuy
[estado] => Yaracuy
[2] => VE-U
[iso_3166-2] => VE-U
)
[22] => Array
(
[0] => 23
[id_estado] => 23
[1] => Zulia
[estado] => Zulia
[2] => VE-V
[iso_3166-2] => VE-V
)
[23] => Array
(
[0] => 24
[id_estado] => 24
[1] => Distrito Capital
[estado] => Distrito Capital
[2] => VE-A
[iso_3166-2] => VE-A
)
[24] => Array
(
[0] => 25
[id_estado] => 25
[1] => Dependencias Federales
[estado] => Dependencias Federales
[2] => VE-Z
[iso_3166-2] => VE-Z
)
)
This is the fix that I want to be sent to the JS file, but in the same way when executing the click action, it shows me an error message "parsererror" and enters the error function of ajax and not the success .