Return of Incorrect Form

Asked

Viewed 76 times

0

Hello!

From a csv file, because after doing submit On the form do I get the return with the correct values? If the $_POST countries are the same $registo[1], wanted to return the $registration[0] and $registo[2] corresponding to thatregisto[1]

<?php
$br = "</br>";
$file = fopen("paises.txt", "r");

$codigo = (isset($_POST['codigo']) ? $_POST['codigo'] : null);
$pais = (isset($_POST['pais']) ? $_POST['pais'] : null);
$capital = (isset($_POST['capital']) ? $_POST['capital'] : null);
$cod = "";
$cap = "";

if (array_key_exists("pais", $_POST)) {
    echo "<table border='1'><th>Pais</th>";
    while (!feof($file)) {
        $registo = fgetcsv($file);

        if ($pais = $registo[1]) {

            $cod = $registo[0];
            $cap = $registo[2];
        }

        echo"<tr>";
        echo"<td>" . $registo[1] . "</td>";
        echo"</tr>";
    }
    echo "</table>";
    fclose($file);
}
?>

<?php
$br = "</br>";

if ($_POST) {
    echo "Codigo do pais escolhido: " . $cod . "<br>";
    echo "Capital: " . $cap . " - " . $registo[1] . "-" . $cod . "<br>";
} else {
    ?>

    <form action="eta15_2teste.php" method="post">
        Pais: <input type="text" name="pais"><br>
        <input type="Submit" name="enviar" value="Enviar">
    </form>

    <?php
}
?>

Always returns the values of $registo[0] and $registo[2] last line of the file

  • someone might try to help?

  • I don’t know if this is it, but your if is wrong "if( $pais = $registo[1] )". You are assigning $registration[1] to $pais

  • @Denisrudneiuza I just wanted when filling the country in the form, to look in the $registration[1] if it existed, if it existed to echo the country’s code and capital

1 answer

1


The problem is in if within your while, it is assigning the value of $registo[1] in the variable $pais every time a line is read in the file, so it displays only the value of the last line, change the assignment signal (=) by a comparison (==), thus the value of the variables $cod and $cap shall be defined only when the condition is satisfied

Browser other questions tagged

You are not signed in. Login or sign up in order to post.