Undefined offset when passing data from a checkbox

1

When executing this code, it indicates Undefined offset. Doing a dump of variables, I discovered that the problem is in a checkbox $informarUP[$fila] , that if it is selected it does not give the error and if it is not selected, it gives the error.

What I do not achieve is that the checkbox has a value if it is not selected.

<?php
//...

foreach($idfila as $fila) {
$pdesid = uniqid('pdes_');		

if(isset($idfila)){
$queryPedidoDesglose = "INSERT INTO pedidos_desglose (id_pedido_desglose,id_pedido, id_pedido_trafico, direccion_inicio, hora_inicio, hora_inicio_hasta, descripcion, hora_rendicion, informar) VALUES ('$pdesid','$peid', '$id_pedido_nuevo', '$destino[$fila]', '$pedidohs[$fila]', '$pedidohshasta[$fila]', '$pedido_desarrollo[$fila]', '$rendicionhs[$fila]', '$informarUP[$fila]')";

if ($mysqli->query($queryPedidoDesglose) === TRUE){}; 


//...
}
}
?>
    
asked by pointup 01.12.2018 в 00:56
source

3 answers

2

Let's start at the beginning, what does the "Undefined offset" error mean:

Imagine that you create an array like the following:

$fruta[0]= “naranja”;
$fruta[1]=”manzana”;

and then somewhere in your code you try to read the value $ fruit [2] for example:

echo $fruta[2];

There's the problem. Since a third value has never been defined for the array $fruta[] , $fruta[2] is outside the range of values of the array that has only assigned values for $fruta[0] and for $fruta[1] . In the PHP error handling system that is "Undefined offset".

Now, analyzing your code, in the line that throws the error you have several array indexes that are variables, some of them surely fall outside the range of values defined for that array, these are:

$destino[$fila]
$pedidohs[$fila]
$pedidohshasta[$fila]
$pedido_desarrollo[$fila]
$rendicionhs[$fila]
$informarUP[$fila]

How to fix it?

You must find which of these values is causing the "Undefined offset" error . One way to investigate what is the problematic index would be to comment on the line that gives you the error and, in different lines of your file, execute:

echo $destino[$fila];
echo $pedidohs[$fila]
echo $pedidohshasta[$fila]
echo $pedido_desarrollo[$fila]
echo $rendicionhs[$fila]
echo $informarUP[$fila]

This way depending on which line you throw the error "Undefined offset" you will know which one or which of those indexes are causing you a "Undefined offset" .

In this case, you should first verify that the value exists in your script, example:

if (isset $informarUP[$fila] ) {
 $informarUP = $informarUP[$fila];
}else{
 $informarUPchecked = ''; // o cualquier otro valor que desees
}

and in the query instead of inserting directly the value that comes from the array $ informeUP [$ row] you insert the variable $ informeUPchecked.

    
answered by 01.12.2018 / 01:19
source
1

For a checkbox to have value when it is not selected you can trick it by before a hidden field with the same name as the checkbox and the value you want if it is not checked.

In this way, if the check is not checked, the hidden field value will be sent.

For example:

<input type="hidden" name="acuerdo" value="0">
<label>Marca si estas de acuerdo: </label><input type="checkbox" name="acuerdo" value="1">
<input type="submit" name="continuar" value="Continuar">

This way you will always get the variable called agreement , with value 1 or with value 0.

It's a trick a little dirty, only advisable to get out of the fast step temporarily.

It would be better to solve the incidence in the script that receives the data, something like this:

 if (isset($_REQUEST['acuerdo']) {
     $acuerdo = $_REQUEST['acuerdo'] ;
 } else {
     $acuerdo = 0;
 }

or in its reduced version:

 $acuerdo = isset($_REQUEST['acuerdo'])?$_REQUEST['acuerdo']:0;
    
answered by 01.12.2018 в 02:33
1

I solved it with empty , validating within foreach .

$informarUP = $_POST['informar'];

foreach($idfila as $fila) {
if(empty($informarUP[$fila])){
$Informar = "NO";
}else{
$Informar = "SI";   
}
}
    
answered by 01.12.2018 в 15:14