Recently I've started to learn web dev in a Udemy course. When I use the code below, the error "Uncaught "SyntaxError: Unexpected token < in JSON at position 0" comes to the console. After looking at it, I realized that the problem was "Notice: Undefined index: accion" followed by the file name and line causing the notice. So, I asked my teaching assistant and he just told me to put "error_reporting(E_ALL ^ E_NOTICE);" in the code, like this:
<?php
**error_reporting(E_ALL ^ E_NOTICE);**
if($_POST['accion'] == 'crear') {
// Creará un nuevo registro den la base de datos
require_once('../funciones/bd.php');
I know that is to change the error reporting level and makes my code work but is not the solution I am looking for.
I've tried to use the function "isset" in different ways and it doesn´t work (I'm probably doing something wrong).
1. if isset(($_POST['accion']) == 'crear') {
2. if isset(($_POST['accion'] == 'crear')) {
I've also tried to set the variable at the beginning, like this:
$accion = $_POST['accion'];
But it doesn´t work either. I don't know what else to do. I've been Googling and I've seen similar things that has been fixed using the function "isset" when using "$_POST" but the only difference is that I use the equal operators (don't know if that has something to do). And as mentioned above, I've also tried to set this variable...
<?php
if($_POST['accion'] == 'crear') {
// Creará un nuevo registro den la base de datos
require_once('../funciones/bd.php');
// Validar las entradas
$nombre = filter_var($_POST['nombre'], FILTER_SANITIZE_STRING);
$empresa = filter_var($_POST['empresa'], FILTER_SANITIZE_STRING);
$telefono = filter_var($_POST['telefono'], FILTER_SANITIZE_STRING);
try {
$stmt = $conn->prepare("INSERT INTO contactos (nombre, empresa, telefono) VALUES(?, ?, ?)");
$stmt->bind_param("sss", $nombre, $empresa, $telefono);
$stmt->execute();
if ($stmt->affected_rows == 1) {
$respuesta = array (
'respuesta' => 'correcto',
'datos'=> array(
'nombre' => $nombre,
'empresa' => $empresa,
'telefono' => $telefono,
'id_insertado' => $stmt->insert_id
)
);
}
$stmt->close();
$conn->close();
} catch(Exception $e) {
$respuesta = array(
'error' => $e->getMessage()
);
}
echo json_encode($respuesta);
}
if($_GET['accion'] == 'borrar') {
require_once('../funciones/bd.php');
$id = filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT);
try {
$stmt = $conn->prepare("DELETE FROM contactos WHERE id = ? ");
$stmt->bind_param("i", $id);
$stmt->execute();
if($stmt->affected_rows == 1) {
$respuesta = array (
'respuesta' => 'correcto'
);
}
$stmt->close();
$conn->close();
} catch(Exception $e){
$respuesta = array(
'error' => $e->getMessage()
);
}
echo json_encode($respuesta);
}
if($_POST['accion'] == 'editar') {
//echo json_encode($_POST);
require_once('../funciones/bd.php');
// Validar las entradas
$nombre = filter_var($_POST['nombre'], FILTER_SANITIZE_STRING);
$empresa = filter_var($_POST['empresa'], FILTER_SANITIZE_STRING);
$telefono = filter_var($_POST['telefono'], FILTER_SANITIZE_STRING);
$id = filter_var($_POST['id'], FILTER_SANITIZE_NUMBER_INT);
try {
$stmt = $conn->prepare("UPDATE contactos SET nombre = ?, telefono = ?, empresa = ? WHERE id = ?");
$stmt->bind_param("sssi", $nombre, $telefono, $empresa, $id);
$stmt->execute();
if($stmt->affected_rows == 1){
$respuesta = array(
'respuesta' => 'correcto'
);
} else {
$respuesta = array(
'respuesta' => 'error'
);
}
$stmt->close();
$conn->close();
} catch(Exception $e){
$respuesta = array(
'error' => $e->getMessage()
);
}
echo json_encode($respuesta);
}
?>
The notice comes in the line for $_POST['accion'] == 'crear'
when trying to delete ($_GET['accion'] == 'borrar'
) and in the line for $_GET['accion'] == 'borrar'
when trying to create ($_POST['accion'] == 'crear'
).