Uncaught ReferenceError: province_name is not defined
I’m having this error and I don’t know why, because I have used the same steps with other methods and everything is ok. What I want to do is to pass a parameter on a onclick function in venta.php from venta.js, and when the btn is clicked the info must be added on the page.
This is the code for venta.php
require_once "../modelos/Disponibilidad.php";
$disponibilidad = new Disponibilidad();
$rspta = $disponibilidad->listarDisponibilidadEstadoDisponible();
$data = array();
while ($reg = $rspta->fetch_object()) {
$data[] = array(
"0" => '<button class="btn btn-warning" onclick="agregarDetalleDisponibilidad(' . $reg->nombre_provincia . ')"><span class="fa fa-plus"></span></button>',
"1" => $reg->nombre_provincia,
"2" => $reg->ciudad,
"3" => $reg->descripcion,
"4" => $reg->fecha_disponible,
"5" => $reg->hora_disponible,
"6" => $reg->estado
);
}
Code for venta.js
function agregarDetalleDisponibilidad(nombreProvincia) {
if (idDisponibilidad != "") {
var filaDisponibilidad =
'<tr class="filasDisponibilidad" id="disponibilidad' + contDisponibilidad + '">' +
'<td><button type="button" class="btn btn-danger" onclick="eliminarDetalleDisponibilidad(' + contDisponibilidad + ')">X</button></td>' +
'<td><input type="hidden" name="iddisponibilidad[]" value="' + idDisponibilidad + '">' + nombreProvincia + '</td>' +
'<td>' + nombreCiudad + '"></td>' +
'<td>' + tipoVehiculo + '"></td>' +
'<td>' + fechaDisponible + '"></td>' +
'<td>' + horaDisponible + '"></td>' +
'</tr>';
contDisponibilidad++;
detalleDisponibilidad++;
$('#detalleSeleccionDisponibilidad').append(filaDisponibilidad);
} else {
alert("Error al ingresar el detalle, revisar los datos de la disponibilidad ");
}
}
Advertisement
Answer
Strings inside of functions need to be quoted. Change your code from
"0" => '<button class="btn btn-warning" onclick="agregarDetalleDisponibilidad(' . $reg->nombre_provincia . ')"><span class="fa fa-plus"></span></button>',
to
"0" => '<button class="btn btn-warning" onclick="agregarDetalleDisponibilidad('' . $reg->nombre_provincia . '')"><span class="fa fa-plus"></span></button>',