未读取上一个选项值

时间:2016-01-06 11:28:06

标签: javascript php html mysql

所以我有一个重大项目的测试代码。这段代码完美地连接到数据库,并且几乎完成了我设置的所有操作。但是当我从下拉列表中选择最后一个值时,它就不会给出它的值,因为没有明显的原因。 这是我想要的一个例子。

enter image description here

这是“bug”的一个例子。

enter image description here

这是完整的代码(连接除外)。

<?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>

如果您对代码有任何疑问,或者您需要我澄清我的问题,请不要羞于告诉我。

P.S:我知道我应该使用mysqli代替myslq_而且还准备好了陈述,但我正在努力关注真正的问题而不是安全问题。如果我的英语不好,请原谅我。

这是请求的html源代码。

<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>

以下是所请求的javascript代码。

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找到了解决问题的更好解决方案。谢谢所有帮助过的人。

1 个答案:

答案 0 :(得分:0)

您的代码中有2个错误,第一个错误是您缺少右括号,这意味着执行时会出现javascript错误:

onchange="reload3(this"

将其更改为

onchange="reload3(this)"

接下来,您有2个名为f1和f2的表单,提交按钮的格式为f1,其中包含1个名为id的输入。因此,当您按下提交按钮时,您将只获得ID。

f2中没有提交按钮,因此您永远不会提交表单,只有onchange()调用一旦您修复了我上面提到的错误,就会使用id和atual进行位置更改。但是如果你有一个提交按钮,你就会丢失id,因为id的格式为f1。

请记住提交按钮,提交带有输入/选择元素的表单。不是其他形式的元素。

最后,您的所有表单操作都是空的并且默认为get,但将它们留空是不好的做法。