Do not read last parameter value

So, I have this test code for a large project. This code connects perfectly to the database and does just about everything I was configured for. But when I select the last value from the drop-down list, it just won’t give me that value without any argument. Here is an example of what I want.

enter image description here

Here is an example of a "mistake."

enter image description here

Here is the full code (except for the connection).

<?php
include_once('ligacao.php');
//Check connection
if (!$link) {
    die("Connection failed: " . mysqli_connect_error());
}


if (!empty($_GET['id'])) {
    $pesq = $_GET['id'];
    $atual = $_GET['atual'];

    //Distrito e concelho
    $sql = ("SELECT distrito,concelho FROM concelhos WHERE codg_cc = '$pesq'");
    $result = mysql_query($sql, $link);

    while ($row = mysql_fetch_assoc($result)) {
        $dist = $row['distrito'];
        $conc = $row['concelho'];
    }

    //echo ($atual);
    if (isset($atual) and strlen($atual) > 0) {
        $sql3 = ("SELECT DISTINCT new_name_freg,cod_freg_new FROM freguesias WHERE cod_freg_new = '$atual'");
        $result3 = mysql_query($sql3, $link);

        $sql2 = ("SELECT old_name_freg,cod_freg_old FROM freguesias WHERE cod_freg_new = '$atual'");
        $result2 = mysql_query($sql2, $link);
    } else {
        $sql3 = ("SELECT DISTINCT new_name_freg,cod_freg_new FROM freguesias WHERE codg_cc = '$pesq' ORDER BY new_name_freg");
        $result3 = mysql_query($sql3, $link);
    }

    mysql_free_result($result);
}

/* $atual=$_GET['atual']; // This line is added to take care if your global variable is off
  if(isset($atual) and strlen($atual) > 0){
  //echo ('hello');
  $sql2=("SELECT old_name_freg,cod_freg_old FROM freguesias WHERE cod_freg_new = '$atual'");
  $result2 = mysql_query($sql2, $link);
  } */

/*
  } // else{$quer="SELECT DISTINCT subcategory,subcat_id FROM subcategory order by subcategory"; }
  ////////// end of query for second subcategory drop down list box ///////////////////////////
 */

mysqli_close($ligar);
?>

<html>
    <head>
        <script type="text/javascript">

            /*function reload(form)
             {
             val=$pesq;
             self.location='Formulario2.php?id=' + val ;
             } */

            function reload3(form)
            {
                var concelho = '<?= $pesq ?>'; //passa a variavel de php para o javascript
//alert(concelho);
//alert(element.defaultValue);
//alert(self.location);
                val2 = (form.value);
                self.location = 'Formulario2.php?id=' + concelho + '&atual=' + val2;

            }

        </script>
    </head> 
    <body onload=enable_text(false);>
        <div >
            <table>
                <form name=f1 action="" >       
                    <td>
                    <tr>
                        <td>ID:</td> <td><input type="number" name="id" value="<?php echo $pesq; ?>"></td>
                        <td><input type="submit" value="Obter informações"></td>
                    </tr>
                    </td>
                </form> 
            </table>        
            <form name=f2 action="" >
                <table> 
                    <tr>
                        <td>Distrito:</td><td ><input type="text" name="distrito" maxlength="25" value="<?php echo $dist; ?>"></td>        
                        <td>Concelho:</td> <td><input type="text" name="concelho" maxlength="25" value="<?php echo $conc; ?>"></td><br>
                    </tr>
                    <tr>
                        <td>Freguesias:</td>
                        <td>Designação atual :</td> <td><select name="atual" onchange="reload3(this)"><option value=''>Selecione uma freguesia</option>                     
                                <?php
                                while ($row3 = mysql_fetch_assoc($result3)) {
                                    echo "<option size=30 selected value=" . $row3["cod_freg_new"] . ">" . $row3["new_name_freg"] . "</option>";
                                }
                                mysql_free_result($result3);
                                ?>
                            </select></td>                      

                        <td>Designação antiga :</td> <td><select name="antiga" onchange="reload3(this"><option value=''>Select One</option>
<?php
while ($row2 = mysql_fetch_assoc($result2)) {
    echo "<option size=30 value=" . $row2["cod_freg_old"] . ">" . $row2["old_name_freg"] . "</option>";
}
mysql_free_result($result2);
?>
                            </select></td>
                        </form>



                    </tr>
                    <tr>    
                        <td>Número da mesa:</td> <td><input type="number" name="mesa" min="1" max="15"></td><br>
                    <td>Localização da mesa:</td> <td><input type="text" name="local" maxlength="40"></td><br>
                    </tr>
                    <td>Todos os eleitores desta frequesia: <input type="checkbox" name=todos onclick="enable_text(this.checked)" ></td>
                    </tr>
                    <tr>
                        <td>Nº de eleitor inicial do caderno </td><td><input type=text name=intini1 maxlength=5></td><td><input type=number name=intini2 maxlength=5></td>
                        <td>Nº de eleitor final do caderno </td><td><input type=text name=intfim1 maxlength=5></td><td><input type=number name=intfim2 maxlength=5></td>
                    </tr>

                    <tr><br>    
                    <td>Nota:</td> <td><input type="text" name="nota" maxlength="40"></td><br>
                    </tr>
                </table>
            </form>
        </div>
    </body>
</html>

If you have any questions about the code, or I need to clarify my question, feel free to tell me about it.

PS: I know that I should use prepared statements mysqliinstead myslq_, but I try to focus on real issues, not security. Forgive me if my English is bad.

Here is the html source on request.

<form name="f2" action="">
   <br>
   <br>
   <br>
   <br>
   <br>
   <table>
      <tbody>
         <tr>
            <td>Distrito:</td>
            <td><input type="text" name="distrito" maxlength="25" value="Faro"></td>
            <td>Concelho:</td>
            <td><input type="text" name="concelho" maxlength="25" value="Albufeira"></td>
         </tr>
         <tr>
            <td>Freguesias:</td>
            <td>Designação atual :</td>
            <td><select name="atual" onchange="reload3(this)">
                  <option value="">Selecione uma freguesia</option>
                  <option size="30" selected="" value="080106">Albufeira e Olhos de Ãgua</option>
                  <option size="30" selected="" value="080104">Ferreiras</option>
                  <option size="30" selected="" value="080102">Guia</option>
                  <option size="30" selected="" value="080103">Paderne</option>
               </select></td>
            <td>Designação antiga :</td>
            <td><select name="antiga" onchange="reload3(this">
                  <option value="">Select One</option>
               </select></td>
         </tr>
         <tr>
            <td>Número da mesa:</td>
            <td><input type="number" name="mesa" min="1" max="15"></td>
            <td>Localização da mesa:</td>
            <td><input type="text" name="local" maxlength="40"></td>
         </tr>
         <tr>
            <td>Todos os eleitores desta frequesia:
               <input type="checkbox" name="todos" onclick="enable_text(this.checked)"></td>
         </tr>
         <tr>
            <td>Nº de eleitor inicial do caderno </td>
            <td><input type="text" name="intini1" maxlength="5"></td>
            <td><input type="number" name="intini2" maxlength="5"></td>
            <td>Nº de eleitor final do caderno </td>
            <td><input type="text" name="intfim1" maxlength="5"></td>
            <td><input type="number" name="intfim2" maxlength="5"></td>
         </tr>
         <tr>
            <td>Nota:</td>
            <td><input type="text" name="nota" maxlength="40"></td>
         </tr>
      </tbody>
   </table>
</form>

Here is the javascript code on request.

function reload3(form)
{
var concelho = '<?= $pesq ?>'; //passa a variavel de php para o javascript
//alert(concelho);
//alert(element.defaultValue);
//alert(self.location);
val2=(form.value);
self.location='Formulario2.php?id=' + concelho + '&atual=' + val2;

}

: , , , JQuery, . , .

+4
1

2 , - , , javascript :

onchange="reload3(this"

onchange="reload3(this)"

f1 f2, f1, 1 id. , , .

f2 , , onchange() id, , , . , id, id f1.

, , /. .

, , , , .

0

Source: https://habr.com/ru/post/1623078/


All Articles