簡體   English   中英

當對相同變量使用“隱藏”和“按鈕”輸入時,為什么會得到2個相等的html GET參數?

[英]Why do I get 2 equal html GET parameters when using 'hidden' and 'button' input for same variable?

我想在網站中使用三種語言-兩個按鈕用於未選擇的語言,而僅一個文本用於所使用的語言-加上兩個按鈕在div上顯示不同的數據(數據庫中的表,此處未顯示)。

一切都是“ button type ='submit'...”。 我讀到我“只需要”添加一個“ input type ='hidden'...”,但是由於未選擇語言的可點擊按鈕,我在URL上得到一個重復的“ lang”參數。

請問正確的方法是什么?

這是代碼,浮動語言欄位於右上角:

<?php session_start();
// connects to the databse
$erroConn = include(".../connector.php");
// converts $_GETs to $php_vars
if (isset($_GET['lang'])) { // language
    $lang = $_GET['lang'];
    leDic($lang); // reads the apropriate language file
} else {
    $lang = NULL;
}
if (isset($_GET['table'])) { // table
    $table = $_GET['table'];
} else {
    $table = NULL;
}
?>

<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <title>Example</title>
    <style>
        .langs { /* makes the language bar float */
            background-color: #90A090;
            position:absolute;
            right:10;
            top:10;
            font-size: 0.75em; /* 10px/16px = 0.625em */
        }
    </style>
</head>
<body>

    <form id="form1" name="form1" method="get" action="">
    <?php
    echo "<h1>".txt('titulo')."</h1>"; // draws the appropriate phrase from the language file
// if don't have a table selected yet
    if (is_null($table)) {
// shows both buttons
        echo "<button type='submit' name='table' value='A'>table A</button><BR>";
        echo "<button type='submit' name='table' value='B'>table B</button><BR>";
    } else {
// if already have a table selected, shows only the others as buttons
        switch($table) {
            case 'A':
                echo "table A<BR>";
                echo "<button type='submit' name='table' value='B'>table B</button><BR>";
            break;
            case 'B':
                echo "<button type='submit' name='table' value='A'>table A</button><BR>";
                echo "table B<BR>";
            break;
        }
    }
// constructs the language bar inside the div 'langs'
    $dir1 = glob('./coisas_txt*'); # all languages available
    echo "<div id='langs' class='langs'>";
    foreach ($dir1 as $fname) {
        $sigla = mb_substr($fname,-2,NULL,'UTF-8');
// draws a darker button for the selected language
        if ($sigla == $lang) {
            echo "<button type='submit' name='lang' value='$sigla'><strong>$sigla</strong></button>";
        } else {
            echo "<button type='submit' name='lang' value='$sigla'>$sigla</button>";
        }
    }
    echo "</div>";
    echo "<div id='tabela'>";
    if (!is_null($table)) {
// shows the data, if any
        switch($table) {
            case 'A':
                echo 'tabela A';
            break;
            case 'B':
                echo 'tabela B';
            break;
        }
    }
    echo "</div>";
    ?>
    </form>
</body>
</html>

謝謝!

這是基本語言選擇和會話存儲的粗略示例

<?php // lang.php

  session_start();

  $validLanguages = array('en','jp','ru');

  // If found on URL, set the session
  if(isset($_GET['lang']) and !empty($_GET['lang']) and in_array($_GET['lang'],$validLanguages))
  {
    $lang = $_SESSION['lang'] = $_GET['lang'];
  }
  // Read from session
  elseif(isset($_SESSION['lang']))
  {
    $lang = $_SESSION['lang'];
  }
  // Default
  else
  {
    $lang = 'en';
  }

?>

<form method="get">
  <input type="submit" name="lang" value="en" <?php if($lang == 'en'){ ?>disabled<?php }?>>
  <input type="submit" name="lang" value="jp" <?php if($lang == 'jp'){ ?>disabled<?php }?>>
  <input type="submit" name="lang" value="ru" <?php if($lang == 'ru'){ ?>disabled<?php }?>>
  <input type="submit" name="lang" value="es" <?php if($lang == 'es'){ ?>disabled<?php }?>>
</form>

<a href="lang.php">Some Page link</a> | <a href="lang.php">Another Page link</a>

好吧,我需要的是兩個單獨的表單,其中一個表單的按鈕作為另一表單的隱藏輸入,反之亦然。

這是代碼,以防萬一有人需要它:

<?php // index.php
session_start();
$errorConn = include(".../dbfile.php");
// If found on URL, set the session
if(isset($_GET['lang']) and !empty($_GET['lang'])) { //and in_array($_GET['lang'],$validLanguages)
    $lang = $_SESSION['lang'] = $_GET['lang'];
} elseif(isset($_SESSION['lang'])) { // Read from session
    $lang = $_SESSION['lang'];
} else { // Default
    $lang = $_SESSION['lang'] = 'BR';
}
leDic($lang);
if(isset($_GET['table']) and !empty($_GET['table'])) { //and in_array($_GET['lang'],$validLanguages)
    $table = $_SESSION['table'] = $_GET['table'];
} elseif(isset($_SESSION['table'])) { // Read from session
    $table = $_SESSION['table'];
} else { // Default
    $table = $_SESSION['table'] = -1;
}
?>

<html lang='BR'>
<head>
    <meta charset='UTF-8'>
    <title>Example</title>
    <style>
        .langs {
            background-color: #90A090;
            position:absolute;
            right:10;
            top:10;
            font-size: 0.75em; /* 10px/16px = 0.625em */
        }
    </style>
</head>
<body>
    <form id="form1" name="form1" method="get" action="">
    <?php
    echo "<h1>".txt('titulo')."</h1>";
    echo "<input type='hidden' name='lang' value='$lang' />";
    echo "<button type='submit' name='table' value='A'>table A</button><BR>";
    echo "<button type='submit' name='table' value='B'>table B</button><BR>";
    echo "<div id='tabela'>";
    if (!is_null($table)) {
        switch($table) {
            case 'A':
                echo 'tabela A';
            break;
            case 'B':
                echo 'tabela B';
            break;
        }
    }
    echo "</div>";
    ?>
    </form>

    <form id="form2" name="form2" method="get" action="">
    <?php
    $dir1 = glob('./coisas_txt*'); # all languages available
    echo "<input type='hidden' name='table' value='$table' />";
    echo "<div id='langs' class='langs'>";
    foreach ($dir1 as $fname) {
        $sigla = mb_substr($fname,-2,NULL,'UTF-8');
        if ($sigla == $lang) {
            echo "<button type='submit' name='lang' value='$sigla'><strong>$sigla</strong></button>";
        } else {
            echo "<button type='submit' name='lang' value='$sigla'>$sigla</button>";
        }
    }
    echo "</div>";
    ?>
    </form>
</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM