Problem with php in vector

1

Good, I have a problem with php, it's a vector form

<!DOCTYPE html>
<html lang="es">
<head>
<title>Ejercicio7.php - Vectores 2</title>
</head>
<body>
<?php 
if ( isset ($_POST['vec']));
{
    $vector =$_POST['vec'];
$n=count ($vector);
$suma=0;
echo "el vector tiene $n elementos <br>";
for ($i=0;$i<$n; $i++)
{
    echo $i = $vector [$i]. "<br>";
        $suma=$suma + $vector [$i];
    }
echo " la suma es ".$suma . "<br";
}

?>
<form action="ejercicio7.php" method= "get">
Numero de Elementos :
<input type="text" name="n" size="5">
<input type="submit" value="Aceptar">

</form>
<br>
<br>

<form action="ejercicio7.php" method="post">
<?php 
if (isset( $_GET['n']))
    $n=$_GET['n'];
else 
    $n=3;
for($i=1;$i<=$n;$i++)
{
    echo $i;
    echo "<input type= 'text' name='vec[]' size='10'>";
}

?>

<input type="submit">


</form>
</body>
</html>

When I execute it I get this:

  

Notice: Undefined index: vec in   C: \ xampp \ htdocs \ ExercisesPHP \ exercise7.php on line 10

the vector has 0 elements

I'm reviewing the code, but I can not find anything

    
asked by marxal 16.12.2016 в 18:48
source

2 answers

1

The problem is simple, you have a ; (semicolon) for others in your if sentence on line 10.

    
answered by 17.12.2016 / 08:17
source
2

I think the error is at the time of writing the if .

if (condicion boleana) {

} else if (opcional) {

} else (opcional) {

}

Your well-written code would look like this:

if (isset ($_POST['vec'])) { //<-- Aquí estaba el error
    $vector =$_POST['vec'];
    $n=count ($vector);
    $suma=0;
    echo "el vector tiene". $n." elementos <br>";
    for ($i=0;$i<$n; $i++) 
    {
        echo $i = $vector [$i]. "<br>";
        $suma=$suma + $vector [$i];

    }
    echo " la suma es ".$suma . "<br";
}
    
answered by 16.12.2016 в 19:18